In dust.js, can you pass a parameter from a sub-template through to a base? - dust.js

In my dust template library, have a 3 levels of templates:
base: {#myparam}{.}{/myparam}
parent: {>base}
child: {>parent myparam="value"}
How might I pass myparam from 'child' to the 'base' ?

Try this:
{! base template !}
{myparam}
then
{! parent template !}
{>base myparam=childparam/}
then
{! child template !}
{>parent childparam="value"/}
Try this jsFiddle: http://jsfiddle.net/smfoote/gZM3H/1/

Related

How should i match the Elmish toNavigable argument types

I'm trying to learn the SAFE Stack at the moment, specifically attempting to handle URL navigation via Elmish; I've followed the example code on the Elmish site that defines a route mapping function and then passes that to the parsePath function.
However, Program.toNavigable expects a Parser<'a> type (a type alias for Location -> 'a) as its first argument, but the sample code (parsePath routes) first argument is a Location -> 'a option.
Obviously i can use function composition to get the correct typing, but it seems like I'm missing something here. Can anyone familiar with URL navigation in Elmish advise?
Well, a Parser<'a option> is a Parser<'a> (just with another 'a), so things should compose just fine.
Say, e.g., that the following type defines all navigation:
type Route = Blog of int | Search of string
Then the parties involved should have the following types:
init: Route option -> Model * Cmd<Msg>
parser: Parser<Route option>
urlUpdate: Route option -> Model -> Model * Cmd<Msg>
and you compose your program thusly:
Program.mkProgram init update view
|> Program.toNavigable parser urlUpdate
|> Program.withReactBatched "elmish-app"
|> Program.run

How to call a module dynamically in Erlang?

Assume I have two modules a.erl and b.erl. Both modules contains the same functions ( in Java I would say "both classes implement the same interface" ).
In module "c.erl" I want to have a function that will return module "a" or "b" ( depends on the parameter )
Here is what I want to have in module c.erl
-module(c)
get_handler(Id) ->
% if Id == "a" return a
% if Id == "b" return b
test() ->
get_handler("a"):some_function1("here were go for a"),
get_handler("a"):some_function2("aaaa"),
get_handler("b"):some_function1("here we go for b")
How can I make this work? I am relatively new to Erlang and don't know how to do it. In Java it would be very obvious, because you just return new instance of the class.
Just have get_handler/1 return the module name as an atom, and then use it to call the desired function:
(get_handler("a")):some_function2("aaaa"),
(get_handler("b")):some_function1("here we go for b").
Note that you need parentheses around the call to get_handler/1 in this case.
A simple version of get_handler/1 for modules a and b could be:
get_handler("a") -> a;
get_handler("b") -> b.
If you have an atom in a variable you can use it as a module name.
So, you could define c:get_handler/1 like this:
get_handler("a") -> a;
get_handler("b") -> b.
Your c:test/0 looks ok, except you need extra brackets, like this:
test() ->
(get_handler("a")):some_function1("here were go for a"),
(get_handler("a")):some_function2("aaaa"),
(get_handler("b")):some_function1("here we go for b").
Then in modules a and b just define a some_function1/1 and some_function/2, for example:
some_function1(Str) ->
io:format("module ~s function some_function1 string ~s~n", [?MODULE, Str]).
some_function2(Str) ->
io:format("module ~s function some_function2 string ~s~n", [?MODULE, Str]).
Edit: You should possibly also define a behaviour if you're going to do this sort of thing BTW, which would mean you would declare in modules a and b something like this:
-behaviour(some_behaviour).
Then create module some_behaviour something like this:
-module(some_behaviour).
-callback some_function1 (String :: string()) -> ok .
-callback some_function2 (String :: string()) -> ok .
This means any module like a and b that declare that they support the behaviour some_behaviour must define those functions, and the compiler will speak up if they don't. The types of the parameters and return value are also defined here for static analysis, etc.

What's wrong with the following FsCheck test

It's probably something very simple, but I'm new to FsCheck and not sure why the below raises the error it does ("Geneflect: type not handled System.Numerics.BigInteger")?
open System.Numerics
type NumericGenerator =
/// Generating BigIntegers (though only in the regular integer range for now)
static member BigInt() =
{ new Arbitrary<System.Numerics.BigInteger>() with
override x.Generator =
Arb.generate<int>
|> Gen.map (fun i -> new BigInteger(i)) }
[<Property>]
let ``Simple test`` (b: BigInteger) =
Arb.register<NumericGenerator> |> ignore
b + 1I = 1I + b
This is using FsCheck with xUnit integration.
FsCheck is trying to generate a BigInteger before calling your test, because the Arb.register call is in your test method itself. It then tries to do that via reflection, which fails.
You can tell FsCheck about your custom arbitrary instance by adding it as a argument to your property.
[<Property(Arbitrary=[|typeof<NumericGenerator>|])>]
Also, you can add the ArbitraryAttribute to the test's enclosing module to register that arbitrary instance for all the properties in the module. See https://github.com/fsharp/FsCheck/blob/master/tests/FsCheck.Test/Runner.fs for some examples.
One final tip - if you are generating a type that's easily converted to/from another already generated type, you can easily create a generate and a shrinker using the Arb.convert method. Something like:
Arb.Default.Int32() |> Arb.convert ...
shoud work.

how do i create a computational expression that takes parameters?

I want to create a couple of computational expressions that would be used to access the database and return a list of items like so (I also have questions in the code comments):
let foo x y z = proc "foo" {
let! cmd = proc.CreateCommand() // can I do this?
do! In "x" DbType.Int32 // would i gain anything by replacing DbType with a union
// type since the names would match actual data types?
do! In "y" DbType.String 15;
cmd?x <- x
cmd?y <- y
use! r = cmd.ExecuteReader() // would this be bad form for creating a workflow builder?
return! r {
let item = MyItem()
do! item.a <- r.GetInt32("a")
do! item.a <- r.GetString("b")
do! item.c <- r.GetDateTime("c")
yield! item
}
}
How can I create a workflow builder such that an instance of it takes a parameter?
let proc name = ProcedureBuilder(connStr, factory) // how do I do this?
Yes, you can do this. You can use computation expression syntax after any expression with a type statically known to expose the right methods. So the following code works (but doesn't do anything particularly interesting):
let f x = async
let v = f "test" { return 1 }
Here, f has type 'a -> AsyncBuilder, so f "test" has type AsyncBuilder and can be followed with computation expression syntax. Your example of let proc name = ProcedureBuilder(connStr, factory) is perfectly fine, assuming that ProcedureBuilder is defined appropriately, though you presumably want name to appear somewhere in the constructor arguments.
The answer from Keith (kvb) is correct - you can use parameterized computation builders. The syntax of computation expressions is:
<expr> { <cexpr> }
So, the builder can be created by any expression. Usually, it is some value (e.g. async) but it can be a function call or even a constructor call. When using this, you would typically define a parameterized builder and then pass the argument to a constructor using a function (as #kvb suggests).
I actually wrote an example of this, not a long time ago, so I can share an example where - I think - this is quite useful. You can find it on F# snippets: http://fssnip.net/4z
The example creates a "special" asynchronous computation builder for ASP.NET MVC that behaves just like standard async. The only difference is that it adds Run member that uses AsyncManager (provided by ASP.NET) to execute the workflow.
Here are some relevant parts from the snippet:
/// A computation builder that is almost the same as stnadard F# 'async'.
/// The differnece is that it takes an ASP.NET MVC 'AsyncManager' as an
/// argumnet and implements 'Run' opration, so that the workflow is
/// automatically executed after it is created (using the AsyncManager)
type AsyncActionBuilder(asyncMgr:Async.AsyncManager) =
// (Omitted: Lots of boilerplate code)
/// Run the workflow automatically using ASP.NET AsyncManager
member x.Run(workflow) =
// Use 'asyncMgr' to execute the 'workflow'
The snippet wraps the construction in a base class, but you could define a function:
let asyncAction mgr = new AsyncActionBuilder(mgr)
And then use it to define asynchronous action in ASP.NET MVC:
member x.LengthAsync(url:string) = asyncAction x.AsyncManager {
let wc = new WebClient()
let! html = wc.AsyncDownloadString(url)
return html.Length }

Where is untyped_to_typed method?

I'm learning to use F# to build WPF applications. I using this as reference. I'm stuck with this one example where the author is trying to
IEnumerable.untyped_to_typed
Its giving me a compile error. Is it being renamed or am I missing something?
Looks like that page is referencing previous versions of the Seq and IEnumerable modules.
Use Seq.cast instead.
#vasu-balakrishnan The code you could use for this is:
//IEnumerable.untyped_to_typed grid.Children
//|> IEnumerable.iter (fun (ctrl : Control) -> ctrl.Margin <- thick)
for child in grid.Children do
(fun (child : Control) -> child.Margin <- thick) |> ignore

Resources