12 Dec 2024 |
calops | oh wait no that doesn't work | 13:50:38 |
Randy Eckenrode | I don’t know if it’s the best way, but this works:
| 13:55:35 |
Randy Eckenrode | * I don’t know if it’s the best way, but this works:
lib.listToAttrs (lib.imap0 (x: y: lib.nameValuePair (toString x) y) x)
| 13:55:47 |
Randy Eckenrode | * I don’t know if it’s the best way, but this works:
list: lib.listToAttrs (lib.imap0 (x: y: lib.nameValuePair (toString x) list) x)
| 13:56:44 |
Randy Eckenrode | * I don’t know if it’s the best way, but this works:
list: lib.listToAttrs (lib.imap0 (x: y: lib.nameValuePair (toString x) y) list)
| 13:56:56 |
Randy Eckenrode | Wish Nix supported function composition …. | 13:57:41 |
calops | oh, imap0 is what i was missing, thanks | 14:05:45 |
calops | what do you mean by function composition ? | 14:16:22 |
Winter | In reply to @reckenrode:matrix.org Wish Nix supported function composition …. be the change you wanna see in the world :3 | 14:33:05 |
Randy Eckenrode | In reply to @calops:tocards.net what do you mean by function composition ? In F#, I could write that as something like:
lib.map0 (toString >> lib.nameValuePair) >> lib.listToAttrs
| 14:44:30 |
Randy Eckenrode | >> is equivalent to . in Haskell. | 14:44:51 |
Randy Eckenrode | * >> is equivalent to . in Haskell. | 14:44:58 |
Randy Eckenrode | Function composition takes two functions. After the first is evaluated, the second is evaluated with the result of the first. | 14:46:11 |
Randy Eckenrode | (f ∘ g) x = f (g x) | 14:47:57 |
Randy Eckenrode | * >> is equivalent to . in Haskell.
Actually, << is equivalent. F# supports both left and right composition. I prefer >> because it reads like constructing a pipeline.
| 14:48:56 |
Randy Eckenrode | In reply to @winter:catgirl.cloud be the change you wanna see in the world :3 They haven’t even accepted the piping operator yet, and composition has potentially way worse impacts on readability. | 14:50:01 |
Randy Eckenrode | Also, I have no bandwidth for stuff like that. 😮 | 14:50:19 |
Randy Eckenrode | * | 14:50:34 |
Winter | In reply to @reckenrode:matrix.org They haven’t even accepted the piping operator yet, and composition has potentially way worse impacts on readability. Lix has ;) (I think) | 14:50:48 |
Randy Eckenrode | (The piping operator is |> . It didn’t come from F#, but it was popularized by it AFAIK.) | 14:52:45 |
Randy Eckenrode | I actually don’t use it much. It reads kind of fugly. I prefer composition or plain function calls usually. It can be really helpful for type inference though. | 14:53:55 |
Bryan | Maybe helpful additional information: |> (or "pipe last") is a standardML-ism, as far as i can tell. WRT composition: |> is function composition, but the functions are curried.
In StandardML (as in Nix and F#), functions are unary so you write a lot of let add = x: y: x + y . This means you can partially apply add trivially with let add2 = add 2 . |> is simply an infix operator that translates add2 4 to 4 |> add2 . For such a trivial example, it's really neither required nor helpful.
This operator really shines when you have something that does repeated list/vector/array processing pipelines: let eleven = [1, 2, 3,4 ] |> List.filter i: i %2 == 0 |> List.map i: i*i | List.fold 0 i: i + i; (or something). IMO it really well encompasses the idea of "we have a bunch of steps and we're doing them in succession to find a final result".
but there are a lot of useless uses of it in the wild too. To each their own tho.
| 17:59:03 |
toonn | They're both technically function composition but the common interpretation of composition is the mathematical one Randy gave. "Pipe" just has the argument order flipped. | 18:28:18 |
Bryan | Oh absoluitely. I'm not indicating otherwise. Just clarifying :) | 18:37:19 |
adamcstephens | I think pipes are rendering not as useful due to the order of arguments for most nixpkgs lib functions | 19:45:33 |
adamcstephens | * I think pipes are rendered not as useful due to the order of arguments for most nixpkgs lib functions | 19:45:42 |
szlend | Iirc the proposal is consistent with how most of nixpkgs.lib works | 19:52:10 |
szlend | so the argument is piped last, vs for example in elixir where it's first | 19:52:58 |
szlend | Most of the nixpkgs lib is design with currying in mind, so that works pretty naturally. Though there are some exceptions to this rule | 19:56:03 |
szlend | removeAttrs comes to mind, but that's a Nix mistake | 19:57:10 |