5 ms·
The pipe operator is awesome because you can use it to “extend” objects without messing with their prototypes. Missing String.titleCase ? Write your own!
by pharmakom 4y ago
The 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
- teg4n_ 4y ago`titleCase(“hello world”)` , how you would already do this seems equally ok to me
- substation13 4y agoAssuming you are only calling one function - if there are more you get into a bit of a mess!
- renke1 4y agoThere was also a different proposal that allows objects to be extended: https://github.com/tc39/proposal-bind-operator 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: User = { … } console.log(User.getDisplayName(renke)); Which makes finding an operation for a certain type easier to find (just write User and trigger autocomplete). The alternative is of course having renameUser (or userRename) and getUserDisplayName (or userGetDisplayname). The prefixed version would make autocomplete easier also.
- galaxyLogic 4y agoJava has https://www.eclipse.org/xtend/ https://www.eclipse.org/xtend/
- pharmakom 4y agoThis is the beauty of pipeline operator. It adds very little extra machinery - it’s just an infix operator that makes working with free functions easier.
- galaxyLogic 4y agoBut in the Hack proposal wouldn't that have to be “hello world” |> titleCase (%) I'm starting to like the F# proposal more now.
- maxloh 4y agoConsider the example code here in Hack, const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast` |> await fetch(%) |> await %.json() |> %.properties.periods[0] In F# it would be much more verbose, const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast` |> fetch |> await |> json |> await |> x => x.properties.periods[0] // or |> { properties : { periods: [result] } } => result Hack will also requires less key strikes for calling functions that have multiple augments.
- pharmakom 4y agoNumber of key strokes is only one design consideration and not even a very important one