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
_
placeholder
#19
Comments
Wouldn't partially-applied operators be more useful?
|
It isn't just operators. Useful in object methods case too ['aids ' ' cancer' 'hitler ' 5123].map(_.toString!).map(_.trim!).filter(_.length > 5) ['aids ', ' cancer', 'hitler ', 5123]
.map(function($0) {return $0.toString();})
.map(function($0) {return $0.trim();})
.filter(function($0) {return $0.length > 5;}) |
The Scala way looks nice and concise, but also is quite complex to properly understand/implement. Coco's solutions are
|
I think the current way (see satyr's post) is pretty good, but I have some ideas for improving it further in the future. |
@paulmillr I've just pushed up prelude.ls to GitHub. With it you can do:
I've added it to http://gkz.github.com/LiveScript/ - you may need to refresh to clear you cache. |
The rule for Scala's See: https://github.com/taku0/placeholder_syntax_for_coffeescript/blob/master/src/placeholder.coffee |
oh. I overguessed this. |
I've added partially applied operators, you can now do
|
@paulmillr also you can use
|
Wow, I love this feature. Amazing! Though I don't think you implemented it for all operators:
|
Yep, pretty awesome thing. What do you think about making this lazy for high performance if more than one |
@michaelficarra glad you like it, and yeah I haven't done all the operators yet. Will do that soon. @paulmillr not really sure what you mean - could you explain further? |
haskell evaluates result not immediately, maybe there's some way to do the same with livescript. but nvm, it ain't a problem. The problem I see is pretty unreadable / hard-to-debug code. See #24. |
@michaelficarra I added the rest of the binary ops |
@paulmillr
equivalent to
and
is equivalent to
unlike So now
Currently on master - I will release a new version of LiveScript soon. |
@gkz yep, just saw the commit, looks cool, thanks. |
What other functionality of Scala's |
any way to write this? edit 3: changed examples to better val b = fn(1, _, 3)
val c = b(2) |
What about:
|
Also: cf. var a = 0
1 to 3 foreach (a += _)
println(a) // 6 |
One thing I just thought ... I'd like to note that this introduces a regression. |
If you do want to change : $ or _, when alone. Can be modifed, but usually refers to some jQuery/zepta & Underscore, so I don't see the point of using |
all make sense, but |
@Nami-Doc I like bare |
I'm affraid $ and _ are over-used |
@Nami-Doc yes, definitely. |
@satyr
|
@paulmillr for your example
There is:
Which compiles to
I think I'm done with this issue. I'll make separate issues for any unresolved items I want to do. |
I guess this is where Scala's approach would work better than Haskell's. Our operators are too inpure and/or whitespace-sensitive. I'm more confident with sticking to |
I must agree :/. |
I just pushed up 5-6 commits. The The placeholder for both partial application and backcalls is the underscore Eg.
and
You can use the underscore as alias to otherwise in a switch statement:
|
yaaaaay thanks dude! looking forward for more stuff it will be doing |
What more could it be used for? The tendons in my right hand have been hurting for several days, so I think I'll need to rest them. No new stuff or docs for a bit. |
The main reason of using nodes
|> map (parseInt _.innerText.trim!toString!) This will replace strange nodes
|> map (_?.innerText) I think i'll find more uses of this in a few days, looking around real code. Scala doesn't has stuff like |
How about this:
This solves a number of issues.
examples of the beauty:
to allow nesting, it could detect parens inside backticks and not close the placeholder-defined function until all inner parens are closed. I think this would be the trivial way to do it by defining the grammar as the one problem I can think of is backtick-delimited infix function within these backquotes:
but I think it's fine to just not allow that infix syntax. |
@adrusi meh. 2complex syntax. placeholder.coffee distinguishes vars and real placeholders. |
@paulmillr but it's not always clear at what point the function begins and ends. how is it too complex. you don't need to have the additional elements that I suggested, I was just saying that they're possible. besides, you often have to surround placeholder-defined functions with parens, which the backticks take care of for you, so often it's the same number of chars too. edit: and, no placeholder.coffee does not distinguish between ALL vars and placeholders. if global._ or window._ is defined in another source file, it won't detect it. and guess what underscore.js does... |
Dunno why, but Scala also allows:
|
@satyr more interesting, I didn't even saw any code with this. All folks use |
@paulmillr I think that the more haskell-like syntax is newer and that the old way remained a best practice to maintain consistency with older code. function's beginning and ending with Edit: functions delimited by
|
also, I'd like to point out that nodes
|> map (parseInt _.innerText.trim!toString!) can already be written in a fairly concise and readable way: nodes
|> map (.innerText.trim!.toString!) >> parseInt which I'd say is even more readable because the order that things are done in flows from left the right. |
real-world code. what I mean is this # Beautiful, innit
# 1
Object.keys(config.files)
|> filter (_ in types)
|> map (config.files[_].order)
|> fold {before: [], after: []}, (memo, array) ->
array ?= []
{
before: memo.before +++ (array.before or [])
after: memo.after +++ (array.after or [])
vendor-paths: config.paths.vendor
}
# 2
files
|> map (_.path)
|> filter (/_test\.[a-z]+$/.test _)
|> map (_.replace //\\//g, '/')
|> map (path) -> path.substring 0, path.last-index-of '.'
|> map ("window.require('#{_}');")
|> lines
# 3
plugins
|> map (_.include)
|> map (call-function-or-pass)
|> filter (_?)
|> fold [], (_ +++ ensure-array _) |
# Beautiful, innit
# 1
Object.keys config.files
|> filter (in types)
|> map (-> config.files[it]) >> (.order) # I created an issue to allow for `(config.file.)` since that is a syntax error ATM.
|> fold do
before: []
after: []
(memo, array ? []) ->
before: memo.before +++ (array.before ? [])
after: memo.after +++ (array.after ? [])
vendor-paths: config.paths.vendor
# 2
files
|> map (.path)
|> filter /_test\.[a-z]+$/.test
|> map (.replace //\\//g, '/')
|> map -> it.substring 0, it.last-index-of '.'
|> map ("window.require('" +) >> (+ "');") # in this case `_` placeholder is indisputably better
|> lines
# 3
plugins
|> map (.include)
|> map call-function-or-pass
|> filter (!== null) # meh...
|> fold [], (-> it +++ ensure-array @@1) # also here if you introduce my proposed syntax:
the advantages of my proposal (I don't want to sound like a salesperson, but you know) are that you can have placeholder-defined functions that will work even if they need a parameter within nested parens, it doesn't overload |
You can do
for
also,
is
Okay, I think I'm done with this now. If you want stuff more complex than what is availible, you can just use |
Thus, for a contrived example
That's it, for real this time. |
What do you think about using it as a placeholder var in languages like Scala? Not sure it's very usable in current way. Or maybe i'm looking the wrong place.
The text was updated successfully, but these errors were encountered: