Why brunch compile everything it found when using function in joinTo - configuration

I don't understand why BrunchJS compile all files (in bower_components) when i use a function like this (CoffeeScript):
modules = ['i18n', 'pager', 'core', 'module-comment']
javascripts:
joinTo:
# Main
'/js/master.js': () ->
paths = ['bower_components/bootstrap/**/*', 'app/**/*']
for o in modules
fs.exists '../../../../workbench/dynamix/' + o, (exists) ->
if exists
paths.push '../../../../workbench/dynamix/' + o + '/public/public/**/*'
else
paths.push '../../../../vendor/dynamix/' + o + '/public/public/**/*'
return paths
I want to test if some path exist, if yes put the complete path in a variable to return it to joinTo. I have successfuly get files in workbench/vendor but it get some undesired files from bower_components (don't specified?!)
I would like to optimize this :
javascripts:
joinTo:
# Main
'/js/master.js':
'bower_components/bootstrap/**/*'
'../../../../workbench/dynamix/i18n/public/public/**/*'
'../../../../workbench/dynamix/pager/public/public/**/*'
'../../../../vendor/dynamix/core/public/public/**/*'
'../../../../workbench/dynamix/module-comment/public/public/**/*'
'../../../../workbench/dynamix/module-love-live-music/public/public/**/*'
'../../../../workbench/dynamix/module-rating/public/public/**/*'
'../../../../workbench/dynamix/module-registration/public/public/**/*'
'app/**/*'
I'm sorry i didn't find documentation to use function in joinTo.
Thanks

A function in a joinTo should take a file path as an argument and return true if the path should be included, false if not. This is described in the anymatch documentation.
Your function appears to always return a truthy value, meaning every path Brunch is watching will be included.
What you might have intended to do is use an IIFE, so the return value of the function (invoked during initial code evaluation) gets assigned to the joinTo. In coffeescript you can accomplish this easily using the do keyword, so instead of starting off your function definition with () -> it'd be do -> instead.

Thank you for your time and your answer.
I make the function to get files and test if the path exists and it work fine.
If this could help someone, I let my magic function here
javascripts:
joinTo:
# Main
'/js/master.js': [
'bower_components/bootstrap/**/*'
'bower_components/unveil/**/*'
'app/**/*'
(string) ->
response = false
modules = ['i18n', 'pager', 'core', 'module-comment']
for o in modules
exists = fs.existsSync unixify('../../../../workbench/dynamix/' + o)
if exists
if unixify(string).indexOf(unixify('../../../../workbench/dynamix/' + o + '/public/public/')) != -1
response = true
else
if unixify(string).indexOf(unixify('../../../../vendor/dynamix/' + o + '/public/public/')) != -1
response = true
return response
]

Related

Corona SDK JSON usage

I have an operating JSON library which I use to load an array of tile IDs. When I double click main.lua directly from file explorer, it runs great, but when I open Corona Simulator and open my project from there or build my project and run it on my testing device, it gives me a null reference error when I attempt to use the data I loaded.
Here is the function to load a table from a JSON file:
function fileIO.loadJSONFile (fileName)
local path = fileName
local contents = ""
local loadingTable = {}
local file = io.open (path, "r")
print (file)
if file then
local contents = file:read ("*a")
loadingTable = json.decode (contents)
io.close (file)
return loadingTable
end
return nil
end
Here is the usage:
function wr:renderChunkFile (path)
local data = fileIO.loadJSONFile (path)
self:renderChunk (data)
end
function wr:renderChunk (data)
local a, b = 1
if (self.img ~= nil) then
a = #self.img + 1
self.img[a] = {}
else
self.img[1] = {}
end
if (self.chunks ~= nil) then
b = #self.chunks + 1
self.chunks[b] = display.newGroup ()
else
self.chunks[1] = display.newGroup ()
end
for i = 1, #data do -- Y axis ERROR IS HERE
self.img[a][i] = {}
for j = 1, #data[i] do -- Z axis
self.img[a][i][j] = {}
for k = 1, #data[i][j] do -- X axis
if (data[i + 1] ~= nil) then
if (data[i + 1][j][k] < self.transparentLimit) then
self.img[a][i][j][k] = display.newImage ("images/tiles/"..data[i][j][k]..".png", k*self.tileWidth, display.contentHeight -j*self.tileDepth - i*self.tileThickness)
self.chunks[b]:insert (self.img[a][i][j][k])
elseif(data[i + 1] == nil) then
self.img[a][i][j][k] = display.newImage ("images/tiles/"..data[i][j][k]..".png", k*self.tileWidth, display.contentHeight -j*self.tileDepth - i*self.tileThickness)
self.chunks[b]:insert (self.img[a][i][j][k])
end
end
end
end
end
end
When it gets to the line for i = 1, #data do it tells me it is trying to access the length of a nil field. Where did I go wrong here?
EDIT: I feel the need to give a more clear explanation of what my problem is. I am getting inconsistent results from this program. When I select main.lua in file explorer and open it with Corona Simulator, it works. When I open Corona Simulator and internally navigate to main.lua, it does not work. When I build the project and test it on my device, it does not work. What I really need is some insight into Corona's JSON library and APK internal directory structure requirements (directory nesting limits, naming restrictions, etc.). If someone thinks of something else that might cause the issue I am having, please bring it up! I am open to anything.
Without seeing the entire error message and not knowing what the value of "path" is it's going to be hard to speculate. But Corona SDK uses four base directories:
system.ResourceDirectory -- Same folder as main.lua and is read-only
system.DocumentsDirectory -- Your writable folder where your data lives
system.CachesDirectory -- for downloaded files
system.TemporaryDirectory -- for temp files.
The last three, while in the simulator are in the project's Sandbox master folder. On device who knows where the folders really are.
In your case, if your JSON file is going to be included in with your downloadble app, your .json file should be in the same folder with your main.lua (or a sub folder) and referenced in system.ResourceDirectory.

Corona Lua call external function

I have block_basic.lua that I want to call another function in touch_input.lua
block_basic.lua does:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
local function touched( event )
-- 'self' parameter exists via the ':' in function definition
print(onScreenTouch, screen_touch, event)
end
From what I am seeing event seems to be correct (a table), screen_touch is also correct. But the function (screen_touch.onScreenTouch) is always nil and I don't know why
In touch_input.lua I simply have:
local function onScreenTouch( event )
-- no 'self' parameter
etc. etc.
Why is it nil? Why can't I call it?
You don't show what you return in touch_input.lua, but if you expect the first two lines of your script to work, it needs to be something like this:
local function onScreenTouch( event )
...
return {
onScreenTouch = onScreenTouch
}
Since you don't get a run-time error on line 2, you may be returning a table already, but you need to make sure that onScreenTouch field of that table points to onScreenTouch function.
Here is how your files should be:
touch_input.lua:
local M = {}
M.onScreenTouch = function( event )
--some code here
end
return M
block_basic.lua:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
print(onScreenTouch, screen_touch, event)
I tested it. It works 100%.
More info:
http://www.coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
http://developer.coronalabs.com/content/modules
http://www.coronalabs.com/blog/2011/07/06/using-external-modules-in-corona/

How do I set a function to a variable in MATLAB

As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points

Cannot pass function handle as an argument of a function

I'm new to Matlab and I'm trying to write custom function in matlab that would take function handle as one of its arguments.
I'm getting this error all the time:
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
Trying to debug I performed following test: I run command x = fminbnd(#humps, 0.3, 1). I proceeded as expected - I got result x = 0.6370.
So I created custom function called train and I copied ALL the code of function fminbnd to the file train.m. The only thing that I changed is the name, so that code of functions fminbnd and train is now identical except for the names.
Now I run both functions with the same argument and the custom function throws error while original fminbnd returns correct answer.
Here is the code:
>> x = fminbnd(#humps, 0.3, 1)
x =
0.6370
>> x = train(#humps, 0.3, 1)
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
Here is header of function train (everything else is copied from fminbnd):
function [xf,fval,exitflag,output] = train(funfcn,ax,bx,options,varargin)
Where is the problem?
Doing a which train showed me that there is a function in the neural network toolbox of the same name.
/Applications/MATLAB_R2009b.app/toolbox/nnet/nnet/#network/train.m % network method
You may be running the nnet train.m rather than the one you think you're running. Are you in the directory containing your train.m? When I made sure I was in the right directory, I got it to work:
>> which train
/Users/myuserid/train.m
>> x = train(#humps,0.3,1)
x =
0.6370
Maybe you can name your file something else like myfminbnd.m instead?
Instead of duplicating the whole fminbnd function, try:
function varargout = myfminbnd(varargin)
varargout = cell(1,nargout(#fminbnd));
[varargout{:}] = fminbnd(varargin{:});
end
this will work as an "alias" to the existing function:
>> fminbnd(#(x)x.^3-2*x-5, 0, 2)
ans =
0.8165
>> myfminbnd(#(x)x.^3-2*x-5, 0, 2)
ans =
0.8165
(you can get the other output arguments as well)

Passing variables into a function in Lua

I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.