[Omake] join function?

Jason Hickey jyh at cs.caltech.edu
Sun Jan 7 00:32:38 PST 2007


On Jan 6, 2007, at 11:34 PM, Erick Tryzelaar wrote:

> Nick wrote:
>> ----------- join.ml begins -----------------
>> let rec join x y = match x, y with
>>   | [], _   | _, [] -> []
>>   | x :: xs, y :: ys -> (x ^ y) :: join xs ys
>>
>>
>> let _ =
>>   let result =
>>     join ["a"; "b"; "c"; "d" ] [ ".x"; ".y"; ".w"; ".z"]
>>   in
>>     List.iter print_endline result
>> ------------ join.ml ends ----------------

Many of the builtin GNU make functions are missing, at least in part  
because they can be coded.  But it would be nice to provide some  
standard compatibility library:)

I would probably write this particular function like this.

    #!/usr/bin/env osh

    nonempty(l) =
       gt($(length $l), 0)

    join(l1, l2) =
       result[] =
       while $(and $(nonempty $(l1)), $(nonempty $(l2)))
          x = $(nth 0, $(l1))
          y = $(nth 0, $(l2))
          l1 = $(nth-tl 1, $(l1))
          l2 = $(nth-tl 1, $(l2))
          result[] += $x$y
       value $(result)

    z[] = $(join a b c d, .x .y .z)
    println($"$z")

prints:

    a.x b.y c.z

> While we're talking about extending the stdlib, I'd also like a  
> python-like range function added, as well as foreachi, in ocaml's  
> iteri style of a function.
>
> Then, while we're there, could we remove the "create-" from "create- 
> map"? I don't think there'd be any conflict, and it would make it  
> more orthogonal to $(array). to add to that, it'd be nice if the $ 
> (map) function could be used to extend maps
>
> How can we call a function like array or the map, but not have any  
> items in it initially? I'd like to be able to do something like this:

For the empty stuff you refer to the standard empty objects directly,

    # x is an empty array
    x[] =
    # y is an empty array
    y = $(Array)
    # z is an empty map
    z = $(Map)

The language is pure--you don't have to worry about clobbering the  
builtins.

> x = $(map b, 1) // python equivalent to {'b': '1'}
> ...
> x = $(map $x, c, 5) // python equivalent to {'b': '1', 'c': '5'}

I agree it might be nice to have some convenient syntax for creating  
maps.  It is worth thinking about.

For these examples at least, it isn't too bad.

    x = $(Map.add b, 1)
    ...
    x = $(x.add c, 5)

> Oh one last thing. I've found that with coding complicated  
> functions, I can get a little overwhelmed with $()s, such as the  
> zip from above. Did I miss anything that would make this a little  
> more simple to work with?

Same for me.  I've often thought it might be nice to have a "standard  
programming" syntax where variables are simple identifiers, and  
strings are explicit.  The meaning wouldn't change, just the syntax.   
In "sane" syntax, the example might look like this.

    join(l1, l2) =
       result[] =
       while and(nonempty(l1), nonempty(l2))
          x = nth(0, l1)
          y = nth(0, l2)
          l1 = nth-tl(1, l1)
          l2 = nth-tl(1, l2)
          result[] += "$x$y"
       result

Someday...  It is just a thought for now.

Jason

--
Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257





More information about the Omake mailing list