r/programming • • Dec 09 '19

Schönfinkelisation and Partial Application

https://functional.christmas/2019/9
32 Upvotes

24 comments sorted by

5

u/[deleted] Dec 09 '19

Just FYI, you don't have to write:

function curriedAdd(a) {
    return function(b) {
        return a + b
    }
}

You can simply do

const add = a => b => a + b

2

u/kvalle Dec 10 '19

Thanks :) I am aware, but felt this syntax better illustrated the difference between uncurried and curried versions of the function.

2

u/simendsjo Dec 10 '19

My guess is the author was explicitly trying to avoid being clever so it's easier to follow along for people not very used to FP.

1

u/shawntco Dec 12 '19

Mix currying with recursion and watch people's heads explode.

8

u/CodeEast Dec 09 '19

I am so tired I read the tittle as Schrodinger and Particle Acceleration.

9

u/simendsjo Dec 09 '19

It's interesting to see how many OOP patterns are implemented easily with functions. The patterns seem to just disappear and we're left with just functions and arguments.

9

u/[deleted] Dec 09 '19

Go play around with a functional language if you haven't!

Functional programming won't necessarily fix all your bugs, give you 10x productivity boost or cook your lunch but it's super useful to be able to mentally switch paradigms and this is why. I've saved so much time simply switching my brain over rather than work with complex OOP patterns.

If you work primarily in .net you can not only use a few functional ideas in C# but I would recommend considering having an F# assembly or two as it can interop with your other c# assemblies quite happily.

1

u/[deleted] Dec 09 '19

Go play around with a functional language if you haven't!

I did. I even did a whole short project in FP at a contracting gig.

I liked it, I learned a lot, but at the end of the day I went back to object land with a bunch of techniques I could easily port over. Dealing with 3 things (Modules, functions and data) instead of two things (Objects and messages) was tiresome.

1

u/simendsjo Dec 10 '19

Interesting way to look at it. Why do you think you found those three things to be more tiresome?

I would say both has "encapsulation, behavior and information", and you need to think about all three. As you say, FP splits these concepts, while OOP blends them using an Object. But I would state that you still need to think about these things in quite a similar way.

Care to expand a bit more about your experience? What type of gig, what language etc?

1

u/[deleted] Dec 10 '19

Interesting way to look at it. Why do you think you found those three things to be more tiresome?

I guess I just prefer passing around objects, not data, then remembering which module operates on that data. List.map f xs just seems inside out compared to xs.map(f), ditto for the abstractions I create myself.

Care to expand a bit more about your experience? What type of gig, what language etc?

Office automation stuff involving a terrible ERP no-one wanted to use, and excel files. The language was F#. I went native, and didn't make any of my own classes, it was all MyModule.T etc. I did fall in love with the Option type, but that's really easy to write as a class.

1

u/simendsjo Dec 10 '19

The List.map, Seq.map etc is a sign that the language is missing higher kinded types. With higher kinded types, as exists in Haskell, you could write map f xs without caring about the module or implementation details of xs. I agree it's quite suboptimal in many languages, but it's the same for OOP unfortunately -- Compile-time reflection and meta-programming is abysmal in all but a few languages.

Needing to remember function is just tooling though, but the tooling is much further along in mainstream languages (which is pretty much just OOP). And it's easier to create code completion tooling when you use the dot-syntax as you've already stated some parts of what you wish to write. But similarly, writing map could give you only valid values for further completions.

But as you also state, you're passing around objects, not just data. Objects contain both behavior and data, so the behavior for the object is easily accessible, but the data is not available for extension.

Alan Perils famously wrote "It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures.", and I'm really beginning to understand this statement after some years of FP.

For F# I can highly recommend https://fsharpforfunandprofit.com/ which helped me getting started. Hope you'll give FP another chance at a later point. There are good reasons why many people like using it, and there are plenty of useful concepts and patterns to bring back to your OOP codebases.

1

u/[deleted] Dec 10 '19

The reason I prefer objects is not really auto-complete. It's the old (maybe unfashionable) idea about objects being a metaphor for an actual computer. Of course deep down it's plain old data + procedures + a this pointer, but it's larger than the sum of its parts.

And more importantly, I don't want to break down an object any further. I want the object as the 'computer metaphor' to be the atomic unit.

This is all orthogonal to mutability you understand. I am big on immutability, objects with constant state returning new objects when you call methods, etc.

-4

u/shevy-ruby Dec 09 '19

This assumes that there is a difference to begin with.

When you can not see the difference then this claim becomes invalid.

I've saved so much time simply switching my brain over rather than work with complex OOP patterns.

I assume you refer mostly to Java. I haven't seen or used any "complex patterns" in ruby.

If you work primarily in .net

There the problem has been identified!

2

u/simendsjo Dec 10 '19

OOP in Java and C# is different from OOP in Ruby, but "everything is an object" and dynamic typing still doesn't remove all OOP design patterns. You keep showing up bashing OOP, FP, Java, C# etc without any further explanation.

In order to have a good and civil discussion, I encourage you to elaborate your arguments in more detail.

0

u/[deleted] Dec 09 '19

What if I were to tell you that calling a constructor with arguments is a concise way to curry multiple functions at once?

1

u/simendsjo Dec 10 '19

I understand where you're going here, but it's not exactly the same. All methods in an object has a hidden parameter this, which is passed automatically. someClass.someMethod() is pretty much syntax sugar for SomeClass.someMethod(someClass). If the someMethod was curried, it would mean someMethod had already been bound to a specific instance of SomeClass. In Javascript, you could do this using bind.

In FP, you could still have a record, a function to construct a record, and functions taking the record as a first parameter. This would be pretty much the same as OOP+constructor.

-3

u/shevy-ruby Dec 09 '19

Hah I said this before - there is no real difference between OOP and functional. People typically tend to use Java when they talk about OOP in these cases, as if Java has a monopoly on the definition of OOP, when in reality Java's OOP is soooooo bad that you just have to weep sadly whenever you read the excessively verbose java examples.

3

u/simendsjo Dec 10 '19

Care to elaborate how OOP and FP is the same?

5

u/n0rs Dec 09 '19

I like that the javascript example is

function curriedAdd(a) {
	return function(b) {
		return a + b
	}
}

and not

const curriedAdd = a => b => a + b;

6

u/nullified- Dec 09 '19

Why?

0

u/n0rs Dec 10 '19

Sorry, sarcasm. I don't particularly like the example.

It's an awkward example because it's using a well known, easy to read language to demonstrate what the elm code is doing but, at the same time, ignoring or making no mention of the fact that you can do a very similar thing in JavaScript already.

On the other hand, if the article did use the arrow function example with JavaScript, the point isn't well made because it's either not that much of a win or it doesn't read like pseudo-code enough for it to be mostly language agnostic.

0

u/[deleted] Dec 09 '19

[deleted]

1

u/nullified- Dec 09 '19

I'm still not sure what this means.

1

u/shevy-ruby Dec 09 '19

What a strange name. Schön = pretty.