xyncro / freya

Freya Web Stack - Meta-Package
https://freya.io
Other
330 stars 30 forks source link

Composing with Freyas and not Freyas #182

Closed Vidarls closed 8 years ago

Vidarls commented 8 years ago

I think I'm doing it wrong, but I currently find that going back and forth between what is inside a Freya computation and what is outside is causing me a lot of pain.

How do I best compose a function within a freya with an argument from outside the freya:

let myFreyaFunc = 
    Freya.init(fun (arg1:string) (arg2:string) -> arg1 + ", " + arg2)

with a normal Non-Freya func I can do:

let myNonFreyaFunc arg1 arg2 =  arg1 + ", " + arg2
//Currently: string->string->string

let composed = myNonFreyaFunc "string1"
//Now becomes string->string

How do I achieve the same in Freya without a lot of ceremony?

The opposite is easy using Freya.map (or operator)

let myFreyaValue = Freya.init("test")

let myNonFreyaFunc value = value + "more"

let result = myNonFreyaFunc <!> myFreyaValue
//or
let result = Freya.map myNonFreyaFunc myFreyaValue
kolektiv commented 8 years ago

I'm not totally sure I see how you mean. It seems like if you have external args, just leaving them external is fine - then your string -> string -> Freya<string> would partially apply and be string -> Freya<string>. If your arguments themselves are Freya functions, you probably want to look at some of the other composition functions (and perhaps operators). https://github.com/freya-fs/freya/blob/master/src/Freya.Core/Operators.fs#L27-L52 is what's available out of the box - >>= and >=> are often quite useful, but as soon as you get in to composing Freya functions, simple partial application is less useful (or possible!).

Vidarls commented 8 years ago

Figured this out be rethinking my components a bit. Will do another issue if required. Thanx for getting back to me :smile: