-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for pipe operator, function currying, and partials #75
Comments
Thanks for the suggestions! We'll probably add the I don't think we'll add LiveScript's operators as functions, instead we have
I also don't think we'll add |
* Add support for |> pipelines Related: #75 * Support yield and await in pipelines
We now have the pipe operator I imagine it's really common to want to pass the piped value in as a first argument to a function, with other arguments given explicitly. What we want is shorthand for
Not sure what's best, but it kind of highlights the limitation of the current |
I think using explicit arrow functions as a way to re-order arguments or specify a local alias is good. That way you can add type annotations and make use of destructuring and everything should compose fine. This way we can combine with existing language features rather than making special purpose syntaxes for every situation. x + 1 |> ($) => $ * $ credentials
|> login
|> getUser
|> ({id}) => register id, 1 |
I have noticed the fun add(x::number, y::number)
x + y
// equals 👇
function add(x: number, y: number) {
return x + y
}
add := (x:: number, y:: number) => x + y
// equals 👇
const add = (x: number, y: number) => x + y
add := (x:: number, y:: number) -> x + y
// equals 👇
const add = curry((x: number, y: number) => x + y)
// 🧂add can be called:
add(1, 2)
add(1)(2)
// usage
input := 4
// pipe version
input
|> add 3
|> subtract 2
|> console.log
// function composition
compute := compose(
console.log
minus 2
add 3
)
compute input
// prints 👉 5 Many monospaced fonts have that array in ligatures. |
@sultan99 Thanks that's a great example for how curried functions can combine with the pipe operator. I think using LiveScript's |
@STRd6, any of these can be! |
What about |
Ideally whichever we choose would have both |
I have checked the ligatures, and we have all of them! == === != !== > --> => ==> ~> >-> I think add := (x:: number, y:: number) ==> x + y
// equals 👇
const add = curry((x: number, y: number) => x + y)
add := (x:: number, y:: number) --> x + y
// equals 👇
const add = curry(
function(x: number, y: number) {
return x + y
}
) In the second example, we can use add := (x:: number, y:: number) -->
console.log(@.name)
x + y |
Guys, it would be sexy syntax! toJson := res => res.toJson()
pick := (key:: keyof T, obj:: T) ==> obj[key]
id := 123
fetch 'api/users/:#{id}'
.then toJson
.catch pick 'message'
.finally console.log |
If we're using the add := (x:: number, y:: number) ==> x + y
const add = (x: number) => (y: number) => x + y
add := (x:: number, y:: number) --> x + y
const add = (x: number) => function (y: number) { return x + y } |
I have seen |
Not sure if this is the right ticket for what I'm after, let me know if not and I'll create a new ticket instead. I use ramda a lot and I'm curious if civet could support a few things natively. Something I do a lot with ramda is create a function using pipe. For example: const doTheThing = R.pipe(
R.pluck('myprop'),
R.map(R.toUpper)
)
console.log(doTheThing([{myprop: 'hello'}])) // outputs [HELLO] My attempt at doing this with civet
doTheThing comes out as const doTheThing = pluck("myprop").map(($3) =>
$3.toUpperCase()
);
// as the first thing in the pipe is a function I would have wanted
const doTheThing = ($) => pluck("myprop")($).map(($3) =>
$3.toUpperCase()
); doTheThing2 comes out as const doTheThing2 = (($4) =>
$4.map(($5) => $5.myprop)).map(($6) =>
$6.toUpperCase()
);
// I would have wanted (note one less paren after $5.myprop, it's added to the last line instead
const doTheThing2 = (($4) =>
$4.map(($5) => $5.myprop).map(($6) =>
$6.toUpperCase()
)); Or perhaps I'm just using it wrong, perhaps the pipe operator |
the operator works only on a start value, you are looking for a function composition operator (flow in fp-ts) which we discussed a bit in the Discord. |
@kabo this might work but I agree that it would be nice to have a cleaner syntax for function composition kinds of things. doTheThing := ($) => (pluck 'myprop' |> .map .toUpperCase())($) |
ok, so I can go ahead and create a ticket for a function composition / flow operator? |
@kabo Sounds good. I don't think we have another issue specifically for composition / flow yet. There are some discussions and maybe an issue that are related but we can link them in later. Thanks for the detailed report and clear description! |
Closing this as:
|
Livescript support piping partials and curry function. Should be nice have this in Civet
The text was updated successfully, but these errors were encountered: