Calling methods from task in Cakefile - coffeescript

I'm setting up a Cakefile that will compile and minify my CoffeeScript and minify my Vanilla libs.
I created different tasks for each case (whether it was a coffee file or not) but I want to combine them into one task.
The problem I'm having is calling a method from the task; I can call a method with no problem under some circumstances, but otherwise I receive
TypeError: undefined is not a function
The object I'm working on looks like
source =
libs: [
'lib/jquery-1.7.1.min.js'
'lib/backbone.js'
'lib/underscore.js'
]
coffees: [
'app/800cart.coffee'
'app/models/coffee/cart.coffee'
'app/models/coffee/contact.coffee'
]
And Im wanting to do this, and I get the error
task 'build', 'Concat, compile, and minify files', ->
for fileType, files of source
concatinate files
concatinate = (files) ->
console.log 'concatinating'
The part that I'm really confused by is if I call the method with a condition it runs fine
task 'build', 'Concat, compile, and minify files', ->
for fileType, files of source
concatinate files if fileType is 'coffees'
concatinate = (files) ->
console.log 'concatinating'
What am I doing wrong here?

The problem is that you're trying to call concatinate before you define concatinate with the line concatinate =. Just move up the declaration, or better yet, move it outside of the task definition.
You're probably used to JavaScript's function concatinate syntax, which automatically moves the function to the top of the scope. CoffeeScript compiles to the concatinate = function syntax instead, mainly because the function cocatinate syntax behaves inconsistently across different JS runtimes (particularly IE). So, CoffeeScript functions simply obey ordinary variable assignment rules.

Related

How to load multiple modules implementing the same behaviour

I do not understand how is one supposed to use multiple modules that each implement the same behaviour since i get this error at compile time:
Function is already imported from
In my case i have two modules implementing gen_event behaviour and i am trying to import them in a third module.
I get the error message whenever i am trying to compile this code:
-module(mgr).
-import(h1,[init/1]). // implements gen_event
-import(h2,[init/1]). // implements gen_event
You can't do that. Import is a simple trick to avoid to write the complete "definition" of a function. It does nothing but just says to the compiler : when you see init(P) in this module, replace with h1:init(P).
Thus it is not possible to import several function with the same name/arity.
For short names, I do not see any benefit to use import.
If you are using module:function with long names, and you want to shorten the lines in the code, it is possible to use macros instead, and there are no limitation (but also few chance that the function name are the same :o):
-define(Func1(Var1,...,VarN), module1:func(Var1,...,VarN)).
-define(Func2(Var1,...,VarN), module2:func(Var1,...,VarN)).
...
?Func1(A1,...,AN);
...
?Func2(B1,...,BN);
Edit
The next example illustrates how it works, first I create the module mod1 as follow:
-module (mod1).
-export ([test/1]).
test(P) ->
case P of
1 -> ok;
2 -> mod2:test()
end.
and I test it in the shell:
1> c(mod1).
{ok,mod1}
2> mod1:test(1).
ok
3> mod1:test(2).
** exception error: undefined function mod2:test/0
4> % this call failed because mod2 was not defined.
4> % lets define it and compile.
mod2 is created as:
-module (mod2).
-export ([test/0]).
test() ->
io:format("now it works~n").
continue in the shell:
4> c(mod2).
{ok,mod2}
5> mod1:test(1).
ok
6> mod1:test(2).
now it works
ok
7>
As you can see, it is not necessary to modify mod1, but only to create and compile mod2 (note that it would be the same if mod2 already exists but the function test/0 is not exported).
If you want to verify that your code is not using undefined function, you can use external tools. As I am using rebar3 to manage my projects, I use the command rebar3 xref to perform this check. Note that calling an undefined function is a simple warning, it is meaningful in the context of application upgrading. This verification is not bullet proof: it is done at build time, this does not guarantee that the modules yo need will be present, with the right version on a production system: it opens a lot more interesting questions about versioning, code loading...

How to call a separate function file in swift 4

I am used to working with Matlab and am struggling to work out how to do some of the same things in swift.
In Matlab, I can create a function in a separate file and save it (in the same directory) as function1.m
and in my second function "function2.m" I can simply call the function with
function1(Input arguments) and it calls this separate file within "function 2".
is there any way to save just a function file in swift and call it in a separate playground or project. I am aware of how to call a function from within the same file in swift, however, if this function is written in a separate file, is there any way to call it, without having to explicitly copy/rewrite the function in the new file.
I have a file saved in the same directory that is called Fibonacci.swift which just contains a function for calculating the Fibonacci series, the function is called fibonacci and within that file i can call it simply by fibonacci(until: Int)
i have tried calling this fibonacci function from a separate file the same way with;
fibonacci (until: Int)
and also tried
Import Fibonacci.swift but this was more of a "I dont know what i'm doing" attempt
neither of the above attempts worked, so any help would be appreciated.
Cheers
- HB
Your function needed to be public:
public func fibonacci(until: Int) {
// Do something
}

Babel is not processing Array.from or 'for ... of' loops

I'm using babel with gulp (.pipe($.babel())), and it seems to work for most things but is not working for Array.from.
It works fine when running babel-node:
$ ./node_modules/babel/bin/babel-node.js
> Array.from
[Function: from]
But when the following code is processed with gulp:
var foo = () => { console.log(Array.from) }
The transpiled source is:
var foo = function foo() {
console.log(Array.from);
};
And the console output when I execute foo is:
undefined
Ended up solving this myself while writing my question, but figured I'd finish and answer myself to help future searchers:
There are certain features of babel that require a polyfill loaded in the browser due to limitations of ES5. This is loaded automatically in babel-node or you can include with with babel-polyfill.
Some of the features requiring the polyfill:
Abstract References
Array destructuring
Async functions
Comprehensions
For of
Array.from
spread

What's the meaning of this line of CoffeeScript?

I was reading through the Journo's source code and I stumbled upon this line of code:
markdown = _.template(source.toString()) variables
What is variables doing here? Is _.template(source.toString()) variables valid stntax at all?
Here's the function wrapping that line of code:
Journo.render = (post, source) ->
catchErrors ->
do loadLayout
source or= fs.readFileSync postPath post
variables = renderVariables post
markdown = _.template(source.toString()) variables
title = detectTitle markdown
content = marked.parser marked.lexer markdown
shared.layout _.extend variables, {title, content}
Yes, it is valid. Parenthesis are optional (sometimes) in CoffeeScript when invoking a function, so it is taking the result of template and invoking it with arguments. It compiles to this JavaScript:
_.template(source.toString())(variables);
From the CoffeeScript documentation:
You don't need to use parentheses to invoke a function if you're passing arguments. The implicit call wraps forward to the end of the line or block expression.
_.template compiles a template specified by source.toString(). A template is a function, which is then called. variables is a parameter for that function (just like postPath post are parameters for fs.readFileSync).
See also the docs for _.template
The question was nicely answered, but to help the OP with future coffee stunts, a great way to anser these koans is to
Go to the coffeescript.org site
Click on "Try coffeescript"
Cut/Paste the puzzle into the coffeescript section
Bingo! You see the generated javascript.
I admit to puzzling over coffeescript at times, and this is abs fab .. and saves headaches.

The difference between scala script and application

What is the difference between a scala script and scala application? Please provide an example
The book I am reading says that a script must always end in a result expression whereas the application ends in a definition. Unfortunately no clear example is shown.
Please help clarify this for me
I think that what the author means is that a regular scala file needs to define a class or an object in order to work/be useful, you can't use top-level expressions (because the entry-points to a compiled file are pre-defined). For example:
println("foo")
object Bar {
// Some code
}
The println statement is invalid in the top-level of a .scala file, because the only logical interpretation would be to run it at compile time, which doesn't really make sense.
Scala scripts in contrast can contain expressions on the top-level, because those are executed when the script is run, which makes sense again. If a Scala script file only contains definitions on the other hand, it would be useless as well, because the script wouldn't know what to do with the definitions. If you'd use the definitions in some way, however, that'd be okay again, e.g.:
object Foo {
def bar = "test"
}
println(Foo.bar)
The latter is valid as a scala script, because the last statement is an expression using the previous definition, but not a definition itself.
Comparison
Features of scripts:
Like applications, scripts get compiled before running. Actually, the compiler translates scripts to applications before compiling, as shown below.
No need to run the compiler yourself - scala does it for you when you run your script.
Feeling is very similar to script languages like bash, python, or ruby - you directly see the results of your edits, and get a very quick debug cycle.
You don't need to provide a main method, as the compiler will add one for you.
Scala scripts tend to be useful for smaller tasks that can be implemented in a single file.
Scala applications on the other hand, are much better suited when your projects start to grow more complex. They allow to split tasks into different files and namespaces, which is important for maintaining clarity.
Example
If you write the following script:
#!/usr/bin/env scala
println("foo")
Scala 2.11.1 compiler will pretend (source on github) you had written:
object Main {
def main(args: Array[String]): Unit =
new AnyRef {
println("foo")
}
}
Well, I always thought this is a Scala script:
$ cat script
#!/usr/bin/scala
!#
println("Hello, World!")
Running with simple:
$ ./script
An application on the other hand has to be compiled to .class and executed explicitly using java runtime.