RE: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread David Allsopp
Valentin ROBERT wrote: I guess you can write it like: let a = (if out then [o] else []) @ (if value then [v] else []) But it's not particularly more pleasant to the eye. Still it reduces the exponential explosion of the code, at a small additional cost (the @), I believe. Actually, it's

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Lin
What about: let a = List.filter (fun x - x) [out; value] Lin On 01/20/2012 02:38 PM, Martin DeMello wrote: let a = match (out, value) with (true, true) - [o; v] | (false, true) - [v] | (true, false) - [o] | (false, false) - [] -- Caml-list

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Valentin ROBERT
Rather (in this case): let a = List.map fst (List.filter (fun x - snd x) [(out, o); (value, v)]) That seems reasonable. On Fri, Jan 20, 2012 at 09:52, Lin mysno...@163.com wrote: What about: let a = List.filter (fun x - x) [out; value] Lin On 01/20/2012 02:38 PM, Martin DeMello

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Jerome Vouillon
On Fri, Jan 20, 2012 at 09:37:17AM +0100, Sebastien Ferre wrote: You can make it even more concise by defining a helping function. let b2l b x = if b then [x] else [];; let a = b2l out o @ b2l value v;; You can also include the list concatenation in the helper function: let

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread oliver
Hello, On Thu, Jan 19, 2012 at 10:38:53PM -0800, Martin DeMello wrote: let a = match (out, value) with (true, true) - [o; v] | (false, true) - [v] | (true, false) - [o] | (false, false) - [] The code looks fine for me. More concise does not always mean

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Fabrice Le Fessant
On 01/20/2012 10:11 AM, Jerome Vouillon wrote: And the parentheses can be avoided by using a right associative application operator: let (@@) f x = f x let a = maybe out o @@ maybe value v @@ [] Since the %revapply primitive is available in SVN 3.12 (next 3.12.2 or 3.13), you will be

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Martin DeMello
I ended up using let (?) x y = if x then [y] else [] in let a = (out ? ...) @ (value ? ...) which had the advantage that it let me inline the definitions of o and v while keeping things readable. martin On Fri, Jan 20, 2012 at 1:08 AM, Valentin ROBERT

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Arnaud Spiwack
I am wondering now if we should also provide the %apply primitive too, to be able to choose the order... The ability to choose the order seems quite valuable to me. Plus, there is a right-associative apply in the non-rev order in Ocaml's distribution (namely in ocamlbuild). -- Caml-list

[Caml-list] Custom let bindings

2012-01-20 Thread Mehdi Dogguy
Hi, I noticed that Alain Frisch tried to add custom let bindings (see r11894 and r11906) but it was reverted later on (see r11960) because no consensus was reached (among OCaml Core team, I guess). AFAIR, I don't remember seeing this on the caml-list. I'd personally vote for its inclusion as I

Re: [Caml-list] Custom let bindings

2012-01-20 Thread Yaron Minsky
We use monads quite a bit, and the lack of a monadic syntax has been a long-running issue for us at Jane Street. I'd love to see some kind of monadic syntax land. I've seen the proposal, and it seems highly plausible. Also, having a special operator (let!) has been proposed as part of this I

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Edgar Friendly
On 01/20/2012 03:37 AM, David Allsopp wrote: Actually, it's possible that with more cases it might be faster - it's eliminating the allocation (at some point) of all the tuples needed for the match case, Doesn't the ocaml compiler not allocate the unnecessary tuples?

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Fabrice Le Fessant
On Fri, Jan 20, 2012 at 2:29 PM, Edgar Friendly thelema...@gmail.com wrote: it potentially eliminates a lot of linear comparisons to find the correct match case (I don't think that the compiler would be able to optimise that to a hash-based or index-based lookup) Isn't the compiler's

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread oliver
On Fri, Jan 20, 2012 at 08:29:09AM -0500, Edgar Friendly wrote: [...] if a then if b then [a;b] else [a] else if b then [b] else [] [...] For me, the original pattern matching code looks much easier to read. Ciao, Oliver -- Caml-list mailing list. Subscription management and archives:

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Edgar Friendly
On 01/20/2012 04:38 AM, oliver wrote: More concise does not always mean better readable or more performant. You apply the same kind of selection for both values. I can't measure readability, but I did throw together a quick benchmark to test the different methods. Please take no offense at

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Edgar Friendly
On 01/20/2012 08:58 AM, oliver wrote: On Fri, Jan 20, 2012 at 08:29:09AM -0500, Edgar Friendly wrote: [...] if a then if b then [a;b] else [a] else if b then [b] else [] [...] For me, the original pattern matching code looks much easier to read. I agree - this was not meant to be a

RE: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread David Allsopp
Edgar Friendly wrote: On 01/20/2012 03:37 AM, David Allsopp wrote: Actually, it's possible that with more cases it might be faster - it's eliminating the allocation (at some point) of all the tuples needed for the match case, Doesn't the ocaml compiler not allocate the unnecessary

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Edgar Friendly
On 01/20/2012 09:12 AM, David Allsopp wrote: Maybe for this case with two variables, yes - but it can't do that indefinitely: as the number of variables increases, the code size increases exponentially. true, the right way to generalize this is to use vuillion's `maybe` function that

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread oliver
On Fri, Jan 20, 2012 at 08:59:31AM -0500, Edgar Friendly wrote: On 01/20/2012 04:38 AM, oliver wrote: More concise does not always mean better readable or more performant. You apply the same kind of selection for both values. I can't measure readability, but I did throw together a quick

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Fabrice Le Fessant
On Fri, Jan 20, 2012 at 2:59 PM, Edgar Friendly thelema...@gmail.com wrote: On 01/20/2012 04:38 AM, oliver wrote: let (|) x f = f x (* no %revapply yet *) let lefessant (out, value) = [] | maybe value v | maybe out o I replayed your bench on 3.12.1+dev, where %revapply is available, and it is

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread oliver
On Thu, Jan 19, 2012 at 10:38:53PM -0800, Martin DeMello wrote: let a = match (out, value) with (true, true) - [o; v] | (false, true) - [v] | (true, false) - [o] | (false, false) - [] [...] Is there a way to get out and value from o and v? Ciao, Oliver

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread oliver
My fold approach version: let folded (out, value) o v = let a = if out then Some o else None in let b = if value then Some v else None in List.fold_left ( fun accum elem - match elem with None - accum | Some x - x :: accum ) [] [ b; a ] or when following the way the other functions are

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread Martin DeMello
On Fri, Jan 20, 2012 at 12:40 PM, oliver oli...@first.in-berlin.de wrote: On Thu, Jan 19, 2012 at 10:38:53PM -0800, Martin DeMello wrote:       let a = match (out, value) with         (true, true)  - [o; v]       | (false, true) - [v]       | (true, false) - [o]       | (false, false) - []

Re: [Caml-list] is there a more concise way to write this?

2012-01-20 Thread oliver
On Fri, Jan 20, 2012 at 10:04:30PM +0100, oliver wrote: My fold approach version: let folded (out, value) o v = let a = if out then Some o else None in let b = if value then Some v else None in List.fold_left ( fun accum elem - match elem with None - accum | Some x - x :: accum