Is this: Object.keys(envars) .map(envar => `${envar}=${envars[envar]}`) .join(' ') |> `$ ${%}` |> chalk.dim(%, 'node', args.join(' ')) |> console.log(%); Really better than: console.log(chalk.dim( `$ ${Object.keys(envars) .map(envar => `${envar}=${envars[envar]}`) .join(' ') }`, 'node', args.join(' ') )); That's the real-world example they have (I reformatted the second one slightly, because it looks better to me). N…
Pipe Operator (|>) For JavaScript
81–90 of 437 posts
Re: Pipe Operator (|>) For JavaScript
#82The pipe operator is awesome because you can use it to “extend” objects without messing with their prototypes. Missing String.titleCase ? Write your own! “hello world” |> titleCase
“hello world” |> titleCase (%)
I'm starting to like the F# proposal more now.
Re: Pipe Operator (|>) For JavaScript
#83Earlier quoted context omitted.
Is either proposal compatible with later adding a backward pipe If so I think that should be a consideration, even if in practice there isn't massive value in a backward pipe operator in javascript. It would be a shame to later want to add it and have to add another layer of kludge and messy syntax.
Yeah, F# has a backward pipe operator, and it works like this: a_value |> (fun a b -> a + b) In F#, this works because of automatic function currying. Not sure how that would apply to Javascript, though. Without function currying, what would this even mean? a_value |> ((a, b) => a + b) ...since there's no currying, you'd just immediately invoke the function that sits "in the middle" with `a` set to `a_value`, but `b`…
The expression you see is evaluated left-to-right. (|>) is a function taking a_value and (fun a b -> a+b) as arguments and applying a_value to the function. This returns a function taking b as an argument (that’s called a partial application). ( is once again a function taking the resulting function and b_value as arguments and applying b_value to its first argument which finally returns the wanted results.
The issue with unary function doesn't exist in F# because every functions can be seen as a unary function returning a function taking one less argument thanks to partial application. That's the beauty of currying.
Re: Pipe Operator (|>) For JavaScript
#84 pipe(one, two, three)
Easy to read. No new syntax. Extendable with arrow functions.Yes, there are some limitations in comparison to Hack Pipes. But those are far outweighed by not messing yet again with the language’s syntax.
Re: Pipe Operator (|>) For JavaScript
#85The pipe operator is awesome because you can use it to “extend” objects without messing with their prototypes. Missing String.titleCase ? Write your own! “hello world” |> titleCase
There was also a different proposal that allows objects to be extended: https://github.com/tc39/proposal-bind-operator Personally, I don't use classes much, but sometimes I think free functions are a little too hard to find, so I tend to experiment with the following pattern. interface User { … } const User = { rename(user: User, newName: string): User { … }, getDisplayName(user: User): string { … } } const renke: Us…
Re: Pipe Operator (|>) For JavaScript
#86JavaScript isn't that kind of programming language. I don't know why people are constantly trying to make it something else.
My pet theory: Front-end devs are generally stuck with JS, but they wish they were able to use other languages, but they can't, and can't convince their managers to use Clojurescript or Purescript, so this is what happens. What's bonkers to me is that ECMA would prefer to keep adding features like this instead of adding a macro system.
Re: Pipe Operator (|>) For JavaScript
#87Earlier quoted context omitted.
> If you're compiling from another language TypeScript isn't another language though. It is the latest official ECMAScript plus type annotations. Only some very, very few, rare, old stuff like enums really is different code. 99% of TypeScript is just "remove the types to get ECMAScript". That TypeScript, the tool, also adds a transpiler is a distraction that made a lot of people believe TS is a different language. Bu…
You're completely missing the point here. If you are transpiling something, you don't need to worry about syntactic sugar in the base language you are transpiling to, only what you're transpiling from.
JS is a fantastic fp language and pipe/compose is commonplace for people writing in that style already. This just adds first class support to the language
Re: Pipe Operator (|>) For JavaScript
#88const isRandNumOdd = Math.round(Math.random() * 100) |> % % 2 |> Boolean; Excruciatingly contrived, but does this sort of arithmetic work in the Hack syntax? I'm genuinely curious, couldn't find any mention of modulo (or remainder) in the proposal.
Gross
Re: Pipe Operator (|>) For JavaScript
#89Is this: Object.keys(envars) .map(envar => `${envar}=${envars[envar]}`) .join(' ') |> `$ ${%}` |> chalk.dim(%, 'node', args.join(' ')) |> console.log(%); Really better than: console.log(chalk.dim( `$ ${Object.keys(envars) .map(envar => `${envar}=${envars[envar]}`) .join(' ') }`, 'node', args.join(' ') )); That's the real-world example they have (I reformatted the second one slightly, because it looks better to me). N…
Very much this. They need a new question on the survey "does JavaScript need new syntax or can we just leave it alone and work on perf/tooling/etc. of the existing stuff without throwing a bunch of new junk in"?
Re: Pipe Operator (|>) For JavaScript
#90[flagged]