These are chat archives for gkz/LiveScript

9th
Jul 2016
sourcevault
@sourcevault
Jul 09 2016 08:43
austinfrey
@austinfrey
Jul 09 2016 16:24
🔥👊
Riley Cat
@WreckedAvent
Jul 09 2016 16:38
why would you need an arbitrary n-flip
oh, rambda doesn't have a backwards compose?
that's silly
sourcevault
@sourcevault
Jul 09 2016 18:31
:D
hopefully it will be helpful for people
sourcevault
@sourcevault
Jul 09 2016 18:36
backward compose is nice for ls do syntax
I read recently that haskell is bringing do composition the way ls does it :D
whoever wrote ls is quite smart
Riley Cat
@WreckedAvent
Jul 09 2016 18:37
haskell's do syntax is for monadic computations
livescript doesn't have anything directly comparable to that, but JS generators could be used for a similar purpose
sourcevault
@sourcevault
Jul 09 2016 18:37
I mean something like this
h = compose do
    add 1
    multiply 2
    add 2
    multiply 4
    add 3
Riley Cat
@WreckedAvent
Jul 09 2016 18:38
Oh, you wouldn't need special do syntax for that, you'd just use compositional operators
sourcevault
@sourcevault
Jul 09 2016 18:38
how would it look ?
Riley Cat
@WreckedAvent
Jul 09 2016 18:40

assuming you had backwards compose defined, like:

h = add 1 << multiply 2 << add 2 << multiply 4 << add 3

different functional languages would have different precedence rules, so you might need to wrap the application in parens

sourcevault
@sourcevault
Jul 09 2016 18:41
buy its still a single line
have you do
Riley Cat
@WreckedAvent
Jul 09 2016 18:41
it would be possible to split on multiple lines if you wanted afaik
sourcevault
@sourcevault
Jul 09 2016 18:41
what about function declaration ?
Riley Cat
@WreckedAvent
Jul 09 2016 18:41
livescript also takes the forward pipe from F#, so there'd be another way to write that
h = (x) ->
  x 
  |> add 1
  |> multiply 2
  |> add 2
  |> multiply 4
  |> add 3
sourcevault
@sourcevault
Jul 09 2016 18:43
h = compose do 
    (x) -> x + 2
    (x) -> x*4
    (x) -> 
        output = x + 1

        return output
actually both work
piping and compose
Riley Cat
@WreckedAvent
Jul 09 2016 18:48
they're just a different way of writing it
livescript also has compositional operators:
h = 
  (-> it + 2) >>
  (-> it * 4) >>
  (->
     output = it + 1
     output)
afaik the parens are required for it to parse correctly
sourcevault
@sourcevault
Jul 09 2016 18:50
var h;
h = compose$(function(it){
  return it + 2;
}, function(it){
  return it * 4;
}, function(it){
  var output;
  output = it + 1;
  return output;
});
function compose$() {
  var functions = arguments;
  return function() {
    var i, result;
    result = functions[0].apply(this, arguments);
    for (i = 1; i < functions.length; ++i) {
      result = functions[i](result);
    }
    return result;
  };
}
doesnt scale
any operator in ls that writes a function like that in my script is a nono
:(
Riley Cat
@WreckedAvent
Jul 09 2016 18:51
well, that's most of the operators in livescript. :smile:
sourcevault
@sourcevault
Jul 09 2016 18:51
its a problem, purescript manages to get around it since its a whole code compilation but then you you deal with all the complexities of purescript
well you can do ls without using most of them :D
the main advantage of ls is arrow functions, whitespaces, and a few other things
Riley Cat
@WreckedAvent
Jul 09 2016 18:52
the pipe works very well and emits no additional noise
sourcevault
@sourcevault
Jul 09 2016 18:52
sorry if that sounds mean :D
I myself have been thinking deeply how to solve it
its a hard problem
ls uses a shortcut - by doing a compilation in the script
Riley Cat
@WreckedAvent
Jul 09 2016 18:53
same thing, cleaner compile:
h = ->
  it
  |> -> it + 2
  |> -> it * 4
  |> ->
    output = it + 1
    output
bit silly in this case, usually with pipe you don't use anonymous functions
sourcevault
@sourcevault
Jul 09 2016 18:54
ahh nice !
one of the reason for using ramda is it scales
you can write your function in one place and can require in all your script
its a trade off
Riley Cat
@WreckedAvent
Jul 09 2016 18:56
I'm personally quite fond of pipe, it's very easy to explain and doesn't require very much functional knowledge
sourcevault
@sourcevault
Jul 09 2016 18:56
me too !
Riley Cat
@WreckedAvent
Jul 09 2016 18:56
it's also very useful with JS libraries
sourcevault
@sourcevault
Jul 09 2016 18:57
I want to write a extention that allows me to use
` -->
for currying
right now i do
_.curry (x,y) -> x + y
using ramda
Riley Cat
@WreckedAvent
Jul 09 2016 18:57
imho just write the currying explicitly
(x) -> (y) -> x + y
sourcevault
@sourcevault
Jul 09 2016 18:57
benchmarks shows its slow
let me show you an exmaple
Riley Cat
@WreckedAvent
Jul 09 2016 18:58
definitely not going to be slower than you calling into another lib to generically curry.
sourcevault
@sourcevault
Jul 09 2016 18:58
update = (Ctrl) -> (eventOb,EventOb) !->

    {events} = Ctrl

    switch eventOb.eventName

    | events.resize => resize-heading Ctrl

    | events.mainscroll => main-scroll Ctrl

    | events.click =>

        if Ctrl.Model.rows[eventOb.selected].selected

            Ctrl.stream.next eventName:events.unselect,selected:eventOb.selected

        else

            Ctrl.stream.next eventName:events.select,selected:eventOb.selected


        highlight-row Ctrl,eventOb.selected
actually I do not think so
the initial lookup is expensive
but after that is fast
if your function body is longer than 10 lines
then currying is expensive
more expensive than generator functions
however if your curried function is 2-4 lines
Riley Cat
@WreckedAvent
Jul 09 2016 19:00
just doubtful the emit be anywhere near as efficient
sourcevault
@sourcevault
Jul 09 2016 19:00
then its faster then even OO techniques :D
Riley Cat
@WreckedAvent
Jul 09 2016 19:00
since it'll have to do very slow things like checking function length
sourcevault
@sourcevault
Jul 09 2016 19:00
when you write short lamdas, its just switching function pointers
but if your function body is long, I am not sure what is happening in V8 but running it 1 million times it seems is really slow
its important for me since so much of my code is based on curring based animation
my uneducated guess is that its inlined
for short lamdas
anyway, I need to go, I need a new job :(
Riley Cat
@WreckedAvent
Jul 09 2016 19:05
okie dokie