Live data from Hacker News

Swift and the Legacy of Functional Programming

realm.io

11–20 of 188 posts

Re: Swift and the Legacy of Functional Programming

#11
post #7

Ask five programmers to define functional programming, get fifteen different answers.

Most people would agree that in functional programming we can pass functions around as first class objects. What we disagree on is the definition of function.

No, wrong. You can pass pointers to functions in C.

Re: Swift and the Legacy of Functional Programming

#12

Ask five programmers to define functional programming, get fifteen different answers.

I always thought FP == pure functions. I guess not?

I would call a language like Haskell "purely functional" and a language like OCaml "impurely functional". A functional programming language to me is a programming language where functions are in charge of data. In a language like Java, data is in charge of functions (broadly speaking). In a language like Prolog, relations are in charge of data. It's all about what perspective the programmer has between the abstraction and the data.

Re: Swift and the Legacy of Functional Programming

#13
post #7

Earlier quoted context omitted.

Most people would agree that in functional programming we can pass functions around as first class objects. What we disagree on is the definition of function.

That's just one aspect of functional programming. By that definition, almost any modern language is functional.

Every modern language is functional. :P But more accurately, every modern language is generally multi-paradigm.

Re: Swift and the Legacy of Functional Programming

#14

Ask five programmers to define functional programming, get fifteen different answers.

If they are truly functional programmers, asking them will always return the same referentially transparent answer. Only side-effecting functions would return different answers at different times :-)

Re: Swift and the Legacy of Functional Programming

#15

Ask five programmers to define functional programming, get fifteen different answers.

Seems to me that's because functional programming continues to evolve (which is great!) and newfangled properties and semantics get folded in.

To me the core is "no side effects" though (for pure FP). It's interesting to see what others consider to be the most salient feature(s).

Re: Swift and the Legacy of Functional Programming

#16
I'm curious, does every else find this

    let persons = names
        .map(Person.init)
        .filter { $0.isValid }
easier to read than this?

    var persons: [Person] = []
    for name in names {
        let person = Person(name: name)
        if person.isValid {
            persons.append(person)
        }
    }
I understand and appreciate the value of compact code, but I find the first one harder to read. A lot of inferred/token based coding is harder for me to mentally parse.

Re: Swift and the Legacy of Functional Programming

#17

I'm curious, does every else find this let persons = names .map(Person.init) .filter { $0.isValid } easier to read than this? var persons: [Person] = [] for name in names { let person = Person(name: name) if person.isValid { persons.append(person) } } I understand and appreciate the value of compact code, but I find the first one harder to read. A lot of inferred/token based coding is harder for me to mentally parse.

  let persons = [ p | n 
is even better, if your programming language has comprehensions.

Re: Swift and the Legacy of Functional Programming

#18

I'm curious, does every else find this let persons = names .map(Person.init) .filter { $0.isValid } easier to read than this? var persons: [Person] = [] for name in names { let person = Person(name: name) if person.isValid { persons.append(person) } } I understand and appreciate the value of compact code, but I find the first one harder to read. A lot of inferred/token based coding is harder for me to mentally parse.

its almost a wash in this case, but when you start having a very complicated flow, the first way becomes easier and easier in relation.

It also depends on the language obviously. In this example, the first example has special tokens for the filter. It doesn't have to be so.

Re: Swift and the Legacy of Functional Programming

#19

I'm curious, does every else find this let persons = names .map(Person.init) .filter { $0.isValid } easier to read than this? var persons: [Person] = [] for name in names { let person = Person(name: name) if person.isValid { persons.append(person) } } I understand and appreciate the value of compact code, but I find the first one harder to read. A lot of inferred/token based coding is harder for me to mentally parse.

The first is much more declarative. When everything in the language looks and acts like that, it's pretty easy to read.

When it's optional, well, sadness ensues.

Re: Swift and the Legacy of Functional Programming

#20

I'm curious, does every else find this let persons = names .map(Person.init) .filter { $0.isValid } easier to read than this? var persons: [Person] = [] for name in names { let person = Person(name: name) if person.isValid { persons.append(person) } } I understand and appreciate the value of compact code, but I find the first one harder to read. A lot of inferred/token based coding is harder for me to mentally parse.

I think part of the answer is that for many functional is a new hotness. The same thing happened with Scala (probably still does). With type inference and functional shortcuts, you can get a lot into one compact space. This is "impressive".

The other thing is that experience brings the ability to track what's going on. The formulation of the answer here is probably new to you. As you get use to this, or Streams for Java, or threading for Clojure, etc, you'll understand it by default.

Post reply on HN