coffeescript-cookbook / coffeescript-cookbook.github.io Goto Github PK
View Code? Open in Web Editor NEWCoffeeScript Recipes, Examples and Tutorials
Home Page: https://coffeescript-cookbook.github.io
License: Other
CoffeeScript Recipes, Examples and Tutorials
Home Page: https://coffeescript-cookbook.github.io
License: Other
evens = (x for x in [1..10] by 2)
evens.every (x)-> x % 2 == 0
This is actually false because tha array starts from 1 by 2... So every number in the array it will be an odd number.
I am going to fork the project and correct this issue then make a pull request.
In my recent ventures into learning more functional styled programming I'm quite fascinated with monads. Both in terms of compositions and readability (A.K.A chaining). This got me thinking on how to pattern this style.
Sure, there are many libraries out there that attempt to make monads through programmatic means (currying I think it's calls). However, in my research the concept for a monad is simple enough one shouldn't need a library to implement.
What I mean is, couldn't there be a pattern one could use to make monads in their programs? I'll discuss a few examples here in hopes to illicit some discussion on possible patterns suitable for the cookbook. (The example are a rough draft I whipped up while writing this).
Simplest example although not very useful (I think).
class Identity
constructor: (@value) ->
@value = @value.value if @value instanceof Identity
bind: (fn) ->
newValue = fn.call(this, @value)
new Identity(newValue)
An example with some practical usage. The Maybe Monad encapsulates a value allowing functions to be composed on it without need for a
class Maybe
constructor: (@value) ->
@value = @value.value if @value instanceof Maybe
bind: (fn) ->
return this unless @value?
newValue = fn.call(this, @value)
new Maybe(newValue)
This is the heart of the issue here, using OOP style classes in a functional / immutable way.
class MyObject
constructor: (@value) ->
@value = @value.value if @value instanceof MyObject
bind: (fn) ->
newValue = fn.call(this, @value)
new MyObject(newValue)
addTwo: ->
@bind =>
@value + 2
This isn't to compete with promises which are much better off being handled via a library but I think there is some advantages to modeling your typical classes as monadic classes providing a chainable and readable way to compose functions on itself and providing immutability to what would otherwise be mutable OOP.
Thoughts? Ideas?
How about going CoffeeKup or Jade (Node.JS) instead of textile to keep things all JS?
http://coffeescriptcookbook.com/chapters/classes_and_objects/cloning
v1 = new Date('1982-2-2')
v2 = clone v1
console.dir v1 + ' ' + v2
Output:
'Tue Feb 02 1982 00:00:00 GMT+0800 (CST) Wed Apr 11 2012 17:56:14 GMT+0800 (CST)'
Looks like pages not generated completely. Please trigger regeneration.
When pulling down a fresh issue from Github, I run bundle install
without any errors, but when running foreman start
I get the following error message:
15:20:25 jekyll.1 | /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/gems/commander-4.1.5/lib/commander/runner.rb:365:in `block in require_program': program version required (Commander::Runner::CommandError)
15:20:25 jekyll.1 | from /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/gems/commander-4.1.5/lib/commander/runner.rb:364:in `each'
15:20:25 jekyll.1 | from /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/gems/commander-4.1.5/lib/commander/runner.rb:364:in `require_program'
15:20:25 jekyll.1 | from /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/gems/commander-4.1.5/lib/commander/runner.rb:52:in `run!'
15:20:25 jekyll.1 | from /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/gems/commander-4.1.5/lib/commander/delegates.rb:11:in `run!'
15:20:25 jekyll.1 | from /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/gems/commander-4.1.5/lib/commander/import.rb:10:in `block in <top (required)>'
Some other terminal commands:
[coffeescript-cookbook.github.com] [master*] which ruby
/Users/mbodmer/.rvm/rubies/ruby-1.9.3-p484/bin/ruby
[coffeescript-cookbook.github.com] [master*] which jekyll
/Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@coffeescript-cookbook/bin/jekyll
[coffeescript-cookbook.github.com] [master*] gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.2.1
- RUBY VERSION: 1.9.3 (2013-11-22 patchlevel 484) [x86_64-darwin13.0.0]
- INSTALLATION DIRECTORY: /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@coffeescript-cookbook
- RUBY EXECUTABLE: /Users/mbodmer/.rvm/rubies/ruby-1.9.3-p484/bin/ruby
- EXECUTABLE DIRECTORY: /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@coffeescript-cookbook/bin
- SPEC CACHE DIRECTORY: /Users/mbodmer/.gem/specs
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-13
- GEM PATHS:
- /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@coffeescript-cookbook
- /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@coffeescript-cookbook/bin
- /Users/mbodmer/.rvm/gems/ruby-1.9.3-p484@global/bin
- /Users/mbodmer/.rvm/rubies/ruby-1.9.3-p484/bin
- /Users/mbodmer/.rvm/bin
- /usr/local/heroku/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
I am running Mac OS X 10.9.2, and have previously installed Jekyll for another project.
i want to tranlate this masterpiece to chinese, what folder structrue should i use?
Browsing the recipes, I found one pattern to be quite common: The extension of build-in native objects.
It's totally fine to back-port ECMAScript 5 methods (as long you make sure, you don't overwrite existing implementations by accident), e.g.:
unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, ""
On the other hand, modifying objects you don't own is considered a bad practice in JavaScript (whereas it's quite common in other languages like Ruby):
Personally, I think those examples should either be reworked or have a note in the "Discussion" section, that this should be used with caution, linking the articles above.
What do you think?
Looking at the following code. According to the recipe on Callback bindings next should be executed. However it is not.
class MyClass:
constructor: () =>
@showCarouselInfo()
@next()
next: () =>
console.log 'fires'
showCarouselInfo: =>
getIndex = (string) =>
number = parseInt(string.match(/\d+/))
$('.ball').on 'click', (e) =>
console.log('fire')
index = getIndex(e.currentTarget.id)
info = $('.carousel-wrapper > p')
@next(info[index])
As defined here: http://coffeescriptcookbook.com/chapters/arrays/removing-duplicate-elements-from-arrays
Array::unique = ->
output = {}
output[@[key]] = @[key] for key in [0...@length]
value for key, value of output
[].unique() evaluates to [null] in the latest version of Chrome.
This can be fixed by using the 'own' keyword to only iterate over output's own properties.
Array::unique = ->
output = {}
output[@[key]] = @[key] for key in [0...@length]
value for own key, value of output
[].unique() evaluates to [] in the latest version of Chrome.
I'm not sure why null is a property of the empty array, but javascript can be weird like that.
I have a question of a basic nature that no tutorial has been able to answer to me. I was working with SourceTree in a local repo pushing to Github.com. At some point I decided to use Github Desktop, so I added the local repo and started to push to Github.com. I did not like this software, so I went back to SourceTree. Opened my project there but changes were not being tracked anymore. I wonder if one can use two git managers for tracking changes or else I don't know what went wrong.
in http://coffeescriptcookbook.com/chapters/math/random-integer
there is this
if not lower?
[lower, upper] = [0, lower]
If lower
does not exist, how can it be considered to use it as upper value?
(I considered using [0, upper]
, but since upper is after lower
in the arguments, It would have its default value anyway. Number.MAX_VALUE
, may be?)
ES6 is gaining a lot of spread. It introduces a lot of new concepts into JS. Here are nice overviews: short, comprehensive.
And people already use those features! Thanks to the effort from such projects as 6to5, es6-transpiler and Traceur, for a modern JS developer who does not use CoffeeScript, there is simply no reason not to use ES6 nowadays. Many modern JS projects are already being written in ES6. Moreover, popular JS frameworks already demand that their users code in ES6 out of the box (this includes Angular 2 and EmberJS).
CoffeeScript is essentially locked at ES5. Some ES6 features are accessible via backticks, but others directly conflict with the CS syntax, including said backticks. With main contributors having explicitly refused to support ES6, CS is doomed to become a thing of the past.
This worries me a lot. I still use CS for all my projects, but i will doubt using CS in a new project.
My problem is that i absolutely adore CS syntax. I worship it. After getting used to CS, i can't stand vanilla CS anymore.
Here's an extract from React sources i saw today (by the way, React is written in ES6):
for (var key in props) {
if (!props.hasOwnProperty(key)) {
continue;
}
style[key] = props[key];
}
This can be rewritten in CoffeeScript as:
for own key, value of props
style[key] = value
JS code is more than 100% larger than CS with zero benefit!
Back to the matter. With CS maintainer having doomed CS to be obsolete, we need a replacement.
I suggest that here we:
It seems my build process is borked. I get many maruku errors about odd references (looks like it's trying to parse markdown inside code block) and the {% highlight %}
code is being ignored.
Is there missing plugins? What is the procedure for testing a build locally before pushing to the code base?
In the example of "The strategies:",
for r in [0..list.length-1]
--->
for r in [0..list.length-2]
We needed to do something about the website's design, but this is not it.
Code boxes with too much contrast, regular text with too little, links are blue on blue, etc...
I propose to regress it to the original dark text on clear background design. All in favor say 'aye'.
There is a mix of title-case and sentence-case for the recipe names in the table of contents. They should be made consistent. I have a preference for title-case. For example this:
Math
Faster Fibonacci algorithm [[sentence]]
A random integer function [[sentence]]
Generating Random Numbers [[title]]
should be:
Math
Faster Fibonacci Algorithm [[title]]
A Random Integer Function [[title]]
Generating Random Numbers [[title]]
I will update the existing sentence-cased recipes to title case if others are in agreement on using title-case.
I attempt use ES6 new feature which is generator function (yield) and read it by for...loop
In javascript i can simple write below :
function* hello(){
var i = 10;
for (var j =0 ;j<i;j++){
yield j
}
}
for(var i of hello()){
console.log(i)
}
In coffeescript:
hello = ()->
for i in [0..10]
yield i
return
for i of hello()->
console.log i
coffee script nothing output.... this is due to compiled js still using for...in instead of for...of, currently i just using embed javascript to solve this problem , could you please fix it ?
Instead of Jekyll, Serve, Thin you can use DocPad - coded in CoffeeScript, and still generates a static website you can use on github, or any node.js host.
The example is wrong at the "Solution" section of "Max Array Value" in chapter "Arrays".
The following does not work:
Math.max [12, 32, 11, 67, 1, 3]
Instead it could be something like:
Math.max.apply null, [12, 32, 11, 67, 1, 3]
Some of the articles mention ruby just to underline that some features in [java|coffee]script are similar to ruby. They assume the reader knows and is accustomed to ruby. I don't think we should go down that path, and should have the syntax arguments for coffeescript's own sake.
Some examples are
http://coffeescriptcookbook.com/chapters/strings/interpolation
http://coffeescriptcookbook.com/chapters/arrays/filtering-arrays
http://coffeescriptcookbook.com/chapters/arrays/mapping-arrays
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.