News for hackers. Stuff that matters.
Re:Microfeatures I'd like to see in more languages by duped on Thursday January 05, 2023 @04:55PM (#34267356)
Python's `with`

Function composition operators eg

    a |> b # Call `b` with `a` as an argument
    b <| a # Same as above, reversed direction
Then you can do something like

    let x : Map = collect <|
      [a, b, c]
        |> map(entries)
        |> flatten
Re:Microfeatures I'd like to see in more languages by masklinn on Thursday January 05, 2023 @05:08PM (#34267551)
> Python's `with`

It's very common already: try-with-resource (java), using (C#), bracket (haskell), unwind-protect (common-lisp), ... though it the latter two it's more of a building block.

Also building block: languages with a convenient and "unrestricted" syntax for anonymous function can just use that e.g. Smalltalk, Ruby, ... in Ruby a "with" is usually just passing a block to the corresponding object's constructor:

    # python 
    with open(...) as f:
        ...

    # ruby
    File::open(...) do |f|
      ...
    end
Re:Microfeatures I'd like to see in more languages by whatshisface on Thursday January 05, 2023 @04:56PM (#34267379)
I don't get it, how are |> and <| any different from parentheses?
Re:Microfeatures I'd like to see in more languages by mikepurvis on Thursday January 05, 2023 @04:57PM (#34267399)
Chaining without nesting.
Re:Microfeatures I'd like to see in more languages by whatshisface on Thursday January 05, 2023 @04:59PM (#34267430)
If you mean without closing parentheses, I think you can also do that in languages like Haskell with non-parenthetical function calls.
Re:Microfeatures I'd like to see in more languages by mikepurvis on Thursday January 05, 2023 @05:03PM (#34267488)
I'm not a Haskell user, but my experience with this in the Nix language is a bit mixed. It definitely works sometimes, but then you get a pileup of parenthesis nesting anyway, because the default is greedy and you have to control which functions get which arguments.
Re:Microfeatures I'd like to see in more languages by nine_k on Thursday January 05, 2023 @07:02PM (#34268893)
Don't $ signs work in Nix the way the work in Haskell?

E.g. https://mmhaskell.com/blog/2021/7/5/function-application-usi...

Re:Microfeatures I'd like to see in more languages by mikepurvis on Friday January 06, 2023 @09:03AM (#34274630)
If so, I've never seen that style used or documented.
Re:Microfeatures I'd like to see in more languages by pavon on Thursday January 05, 2023 @07:33PM (#34269199)
It also allows you to invert the order of the calls so they are written in the order they occur. Instead of paint(sand(cut(measure(wood)))), you can write wood | measure | cut | sand | paint, which is easier to read, especially if it splits over multiple lines or has additional arguments:

paint(sand(cut(measure(wood, 12), 40, :WZ), 220), :red)

wood | measure(12) | cut(40, :WZ) | sand(220) | paint(:red)

IIRC you can do that in Haskell as well, but I forget the name of the feature. Many OOP libraries have started to adopt a chained method call style similar to this, but it is nice to be able to do with any function.

Re:Microfeatures I'd like to see in more languages by wbadart on Thursday January 05, 2023 @11:01PM (#34270803)
In Haskell, if all the functions in the pipeline are pure, I'd probably write that as

    wood
    & measure 12
    & cut 40 WZ
    & sand 220
    & paint Red
See https://hackage.haskell.org/package/base-4.17.0.0/docs/Data-...
Re:Microfeatures I'd like to see in more languages by duped on Thursday January 05, 2023 @06:35PM (#34268581)
Call/Pipe operators are just sugar for normal function calls. It's nice because adding or removing a call doesn't require balancing parentheses. It's helpful for writing stream or sequence/iterator based code and throwing debug utilities in the middle.

It's less of an issue if your language has UFCS or other postfix function call syntax like mentioned in this thread, but if you don't this is nice to have.

Re:Microfeatures I'd like to see in more languages by jbverschoor on Thursday January 05, 2023 @05:14PM (#34267628)
I was under the impression that Elixir introduced this.
Re:Microfeatures I'd like to see in more languages by masklinn on Thursday January 05, 2023 @05:30PM (#34267818)
Elixir has forward pipes, but didn't invent them.

For instance Racket and Clojure have threading macros, which are more flexible as they're just macros (Clojure's `->` is equivalent to Elixir's pipe operator, but `->>` will fill in the last parameter rather than first, and `-->` lets you use a keyword to define where the parameter is inserted in each call).

Haskell let anyone who wants define their own pipe operator, historically you had to BYO, which wasn't exactly hard:

     (|>) = flip ($)
or

     x |> f = f x
would do (modulo fixity), but today it's provided by default as "(&)".
Re:Microfeatures I'd like to see in more languages by int_19h on Friday January 06, 2023 @02:40AM (#34272013)
The earliest I've seen |> specifically in stdlib was in F#, which still predates Elixir by several years.
Re:Microfeatures I'd like to see in more languages by dmulligan on Friday January 06, 2023 @08:09AM (#34274207)
It was present in SML codebases earlier than F# (it's used everywhere in the Isabelle codebase, for example, and that's where I first came across it).
Re:Microfeatures I'd like to see in more languages by gpderetta on Friday January 06, 2023 @07:28AM (#34273933)
'with' is cool, but it is annoying that it creates a new scope. And then you have ExitStack if your lifetimes do not neatly map to scopes. And AsyncExitStack and async with if you happen to be in an async function.

I prefer RAII.