infinisil changed the topic of #nix-lang to: Channel for discussing Nix as a language - https://nixos.org/nix/manual/#chap-writing-nix-expressions - Logs: https://logs.nix.samueldr.com/nix-lang/
lukegb has quit [Quit: ~~lukegb out~~]
lukegb has joined #nix-lang
Dotz0cat has joined #nix-lang
lukegb has quit [Quit: ~~lukegb out~~]
ris has quit [Ping timeout: 240 seconds]
<infinisil> > findFirst = pred: default: list: let index = iterateUntil (i: i >= lib.length list || pred (lib.elemAt list i)) (i: i + 1) 0; in if index >= lib.length list then default else lib.elemAt index
<{^_^}> findFirst defined
<infinisil> > findFirst (x: x > 10) "nope" [ ]
<{^_^}> "nope"
<infinisil> > findFirst (x: x > 10) "nope" [ 1 5 6 72 4 ]
<{^_^}> <PRIMOP-APP>
<infinisil> > findFirst = pred: default: list: let index = iterateUntil (i: i >= lib.length list || pred (lib.elemAt list i)) (i: i + 1) 0; in if index >= lib.length list then default else lib.elemAt list index
<{^_^}> findFirst defined
<infinisil> > findFirst (x: x > 10) "nope" [ 1 5 6 72 4 ]
<{^_^}> 72
<infinisil> Trying to see what a builtins.iterateUntil could be used for (currently just implemented non-ideally in Nix itself)
<infinisil> > attrByPath = attrPath: default: e: iterateUntil (s: s.i >= lib.length attrPath) (s: { i = s.i + 1; v = s.v.${lib.elemAt attrPath s.i}; }) 0
<{^_^}> attrByPath defined
<infinisil> > attrByPath [ "foo" "bar" ] "nope" { foo.bar = 10; }
<{^_^}> value is an integer while a set was expected, at (string):83:54
<infinisil> > attrByPath = attrPath: default: e: iterateUntil (s: s.i >= lib.length attrPath) (s: { i = s.i + 1; v = s.v.${lib.elemAt attrPath s.i}; }) { i = 0; s = e; }
<{^_^}> attrByPath defined
<infinisil> > attrByPath [ "foo" "bar" ] "nope" { foo.bar = 10; }
<{^_^}> { i = 2; v = <CODE>; }
<infinisil> > attrByPath = attrPath: default: e: let result = iterateUntil (s: s.i >= lib.length attrPath || ! s.v ? ${lib.elemAt attrPath s.i}) (s: { i = s.i + 1; v = s.v.${lib.elemAt attrPath s.i}; }) { i = 0; s = e; }; in if result.i >= lib.length attrPath then default else result.v
<{^_^}> attrByPath defined
<infinisil> > attrByPath [ "foo" "bar" ] "nope" { foo.bar = 10; }
<{^_^}> attribute 'v' missing, at (string):83:99
<infinisil> > attrByPath = attrPath: default: e: let result = iterateUntil (s: s.i >= lib.length attrPath || ! s.v ? ${lib.elemAt attrPath s.i}) (s: { i = s.i + 1; v = s.v.${lib.elemAt attrPath s.i}; }) { i = 0; s = e; }; in if result.i >= lib.length attrPath then default else result.s
<{^_^}> attrByPath defined
<infinisil> > attrByPath [ "foo" "bar" ] "nope" { foo.bar = 10; }
<{^_^}> attribute 'v' missing, at (string):83:99
<infinisil> > attrByPath = attrPath: default: e: let result = iterateUntil (s: s.i >= lib.length attrPath || ! s.v ? ${lib.elemAt attrPath s.i}) (s: { i = s.i + 1; v = s.v.${lib.elemAt attrPath s.i}; }) { i = 0; v = e; }; in if result.i >= lib.length attrPath then default else result.v
<{^_^}> attrByPath defined
<infinisil> > attrByPath [ "foo" "bar" ] "nope" { foo.bar = 10; }
<{^_^}> "nope"
<infinisil> Ugh
<infinisil> > attrByPath [] "nope" { foo.bar = 10; }
<{^_^}> "nope"
<infinisil> > attrByPath = attrPath: default: e: let result = iterateUntil (s: s.i >= lib.length attrPath || ! s.v ? ${lib.elemAt attrPath s.i}) (s: { i = s.i + 1; v = s.v.${lib.elemAt attrPath s.i}; }) { i = 0; v = e; }; in if result.i >= lib.length attrPath then result.v else default
<{^_^}> attrByPath defined
<infinisil> > attrByPath [] "nope" { foo.bar = 10; }
<{^_^}> { foo = <CODE>; }
<infinisil> > attrByPath [ "foo" ] "nope" { foo.bar = 10; }
<{^_^}> { bar = 10; }
<infinisil> > attrByPath [ "foo" "bar" ] "nope" { foo.bar = 10; }
<{^_^}> 10
<infinisil> > attrByPath [ "foo" "bar" "baz" ] "nope" { foo.bar = 10; }
<{^_^}> "nope"
<infinisil> Nice
<infinisil> Though, not super nice
<infinisil> While this iterateUntil implementation doesn't need to use lib.tail/lib.head like the current one, this one needs a whole bunch of attributes
rajivr has joined #nix-lang
ddellacosta has quit [Ping timeout: 240 seconds]
tilpner_ has joined #nix-lang
tilpner has quit [Ping timeout: 260 seconds]
tilpner_ is now known as tilpner
kalbasit has quit [Remote host closed the connection]
kalbasit has joined #nix-lang
kalbasit_ has joined #nix-lang
kalbasit has quit [Ping timeout: 256 seconds]
kalbasit has joined #nix-lang
kalbasit_ has quit [Ping timeout: 240 seconds]
kalbasit_ has joined #nix-lang
kalbasit has quit [Ping timeout: 240 seconds]
kalbasit_ has quit [Ping timeout: 240 seconds]
__monty__ has joined #nix-lang
sphalerite has quit [Quit: brb rebooting]
lukegb has joined #nix-lang
sphalerite has joined #nix-lang
tilpner_ has joined #nix-lang
tilpner has quit [Ping timeout: 240 seconds]
tilpner_ is now known as tilpner
tilpner_ has joined #nix-lang
tilpner has quit [Ping timeout: 260 seconds]
tilpner_ is now known as tilpner
kalbasit has joined #nix-lang
__monty__ has quit [Quit: leaving]
rajivr has quit [Quit: Connection closed for inactivity]
ris has joined #nix-lang
clever has quit [Ping timeout: 256 seconds]
tilpner_ has joined #nix-lang
tilpner has quit [Ping timeout: 260 seconds]
tilpner_ is now known as tilpner
ddellacosta has joined #nix-lang
evanjs has quit [Ping timeout: 264 seconds]
evanjs has joined #nix-lang
ddellacosta has quit [Quit: WeeChat 2.8]
tilpner has quit [Ping timeout: 258 seconds]
tilpner has joined #nix-lang