Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 12, 2012 15:55
Show Gist options
  • Save cowboy/3332446 to your computer and use it in GitHub Desktop.
Save cowboy/3332446 to your computer and use it in GitHub Desktop.
LiveScript: language design?
// Re. the LiveScript <-! operator (et al)
// http://gkz.github.com/LiveScript/
function getLiveScriptOperator() {
var op = '';
for (var i = 0; i < 3; i++) {
op += String.fromCharCode(33 + Math.floor(15 * Math.random()));
}
return op;
}
@michaelficarra
Copy link

You're doing it wrong.

concatMap (-> do it) <| replicate 3 -> String.fromCharCode 33 + floor 15 * Math.random()

@cowboy
Copy link
Author

cowboy commented Aug 13, 2012

Yes, much more readable.

@gkz
Copy link

gkz commented Aug 13, 2012

Boom:

[String.fromCharCode 33 + floor 15 * Math.random! for i to 2] * ''

@gkz
Copy link

gkz commented Aug 13, 2012

[String.fromCharCode 33 + floor 15 * Math.random! for i to 2] * ''

(You can't edit Gist comments? That's stupid.)

@pvdz
Copy link

pvdz commented Aug 13, 2012

Can't honestly say whether this is a joke or not. In case it's not, there is no way in hell I can translate the original gist into either of the two.

Is -> do it like function? <| ehr, the subject of what should be "done"? But then what is the -> function like arrow doing there? And replicate is "repeat"?

And what is the * '' supposed to mean? From the original code I'd have to guess that's "concatenate this list". But I'd never have figured that out otherwise.

Also, to in from x to y is inclusve x, exclusive y. So assuming i starts at 0 (would certainly confuse a Lua coder), you seem to be looping two times, not three as the original one.

Point is, I really don't mind all these new flavors. But if you're dead set on creating a new language, at least make sure people can read it. Ask your mom. If she can't make heads or tails from it, you're doing it wrong.

As you were! :)

@gkz
Copy link

gkz commented Aug 13, 2012

Hey @qfox, yes it is a joke. It is an exercise in code golf to see what is the shortest definition.

An explanation of:

[String.fromCharCode 33 + floor 15 * Math.random! for i to 2] * ''

First,

[String.fromCharCode 33 + floor 15 * Math.random! for i to 2]

is a list comprehension. It is an expression which evaluate to a list. It is equivalent to:

result = []
for i to 2
    result.push String.fromCharCode 33 + floor 15 * Math.random!
result

As the from parameter is omitted in the for loop, it is assumed to be 0. to means to and including that number, use til to mean up until (but not including) that number.

[...] * ''

simply means

[...].join('')

@vendethiel
Copy link

@michaelficarra (do) works too, plus you can change your <| with a , here.
My version also is shorter: '-!+'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment