4 ms·
Sometimes it's nice to be able to do piping: object | dothis | dothat | dosomething I suppose the piping operator is infix. I feel like some of the advantag
by ogogmad 5y ago
Sometimes it's nice to be able to do piping:
object | dothis | dothat | dosomething
I suppose the piping operator is infix. I feel like some of the advantages of the notation
object.f()
over
f(object)
in object oriented programming are due to this.
- andreareina 5y agoAn interesting feature of D is Uniform Function Call Syntax (UFCS) where any function with first parameter of type T can be called as a method on T: https://dlang.org/spec/function.html#pseudo-member https://dlang.org/spec/function.html#pseudo-member
- xigoi 5y agoOther languages I know of that have this feature are Nim, Koka and VimScript.
- codethief 5y agoThat is indeed an interesting feature! I wish more languages implemented it! In Python, of course, one argument against implementing it is that there already is the getattr magic method (and the getattribute one), so even if it'd be possible to implement UFCS without breaking changes, it would at least complicate the way attribute access works even further. But, of course, one could always introduce a new operator/token (instead of ".").
- runevault 5y agoI love |> in f# because of this type of thing, piping operations together instead of nesting or the like just reads so much cleaner. And yes in cases where they are all methods on the type you can have the function return self and then do value.Func1().func2().func3().etc() but only if they are methods on the class. But I also don't care for arbitrary infix operators (which I'm pretty sure f# supports but I haven't used it in a way where I run into them). I know I'm crazy but I almost wish we did away with ALL infix operators, even stuff like +/-* and just do everything as functions. You know, like Lisp :P Edit: I guess I should clarify as it sounds back and forth. For traditional stuff everything functions, with exceptions for things like piping that are purely about chaining operations together (both |> and >> in f# are examples of infix that's specific use case is different enough from say +-/* I think they warrant it).
- eyegor 5y agoF# does allow custom infix operators, but of course prefix + piping is generally cleaner. Only time I've used custom infix are for implementing a "null coalesce" operator and for overloading math ops for vectors and matrices. The null coalescing is generally only used for code that interops with c# since f# offers non null guarantees for most types.
- runevault 5y agoI can see wanting null coalesce in interop scenarios. I know I use it a lot in C#.
- ziml77 5y ago> but only if they are methods on the class C# got around this by introducing extension methods. You put static methods inside any static class whose namespace you've imported and label the first parameter with `this` before the type name. Then you're able to call that static method as if it was an instance method on anything that is or implements the type of the first parameter. Basically it's an explicit version of D's unified function call syntax. I think the pipeline operator is nicer, but extension methods aren't a bad solution.
- runevault 5y agoSure, you can fake it to get to the same place. And I've abused extension methods a lot, including on interfaces before c# made default implementations on interfaces a more acceptable thing with c#... 9 I think?
- OJFord 5y ago> I suppose the piping operator is infix. It's more like composition isn't it? object | dothis | dothat Is pretty much dothat(dothis(object)) If stdout is considered the return value at least. The pipe itself is just a sort of.. notation that a postfix function follows? Whereas in mathematics the function is prefixed, and the notation itself is infix and optional. (That's a knowingly rough and lazy description that any mathematician will hate...)
- kragen 5y agoI think dothat(dothis(object)) is more like < object dothis | dothat or dothis object | dothat while the full composition object | dothis | dothat is more like dothis(dothat(object(stdin_and_everything_else)))
- visarga 5y agoIs it possible to do this in Python, such as: instead of: len(my_list) to do: my_list | len of course you can still do: my_list.__len__() but it's ugly