Live data from Hacker News

Functional Language Features: Iterators and Closures

doc.rust-lang.org

1–10 of 40 posts

Re: Functional Language Features: Iterators and Closures

#2
> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth.

> ...

> Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well.

What is it about pattern matching and enums that associates them with functional programming? Because going by the description of functional programming above, I don't see how they fit in. Is it just that pattern matching and enums were first popularized by certain functional languages?

Re: Functional Language Features: Iterators and Closures

#3
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

I believe, on the pattern matching side, it has to do with the fact that it's a higher level feature that feels much more declarative. Inside of writing a sequence of instructions to check whether a list is 3 and the first two are equal, one writes the declarative syntax:

  (define (f x)
    (match x
      [`(,x ,x ,y) y]
      [_ #f]))

Re: Functional Language Features: Iterators and Closures

#4
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

My take: sum and product types exist in various type theories (enums with their elimination rule being pattern matching). Some of the first type theories to feature them were for the lambda calculus, which functional programming is based on. As a result, they've been ubiquitous in functional programming, and have become associated with it heavily.

Re: Functional Language Features: Iterators and Closures

#5
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

One of my teachers liked to use the term value-oriented programming for the FP subset that does not make use of functions for data representations, or even first class functions beyond combinators like 'map'. I've always liked that term and that concept, and I do believe value-oriented programming, understood as programming with pure functions and simple values as much as possible, is 75% of the value of full functional programming. And I say that as someone who has both used Haskell as my primary language for over a decade, and implemented my own functional language.

Re: Functional Language Features: Iterators and Closures

#6
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

> first popularized by certain functional languages

Basically that.

People associate all kinds of different thinks with the term functional programming. The only think in common is that it's focused on functions, including first class functions.

For other thinks including things like (strict) immutability, lazy evaluation, syntax, algebraic data types, linear typing, etc. it is different from person to person weather or not they think about this as part of FP or just another think which happens to be part of the FP language they use/looked into.

Re: Functional Language Features: Iterators and Closures

#7
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

Yes -- sum types have historically not been present in imperative languages, whereas products types have essentially always been present. Imperative languages have correspondingly good support for various uses of product types, but even if sum types can be encoded, you generally can't abstract well over them due to the nonexistent language support for their use.

C does have unions, but you can't distinguish which field is safe to read without additional information. Discriminated unions are a design pattern built on top of the language feature to give a true sum type, but since they aren't core to the language, pattern matching still isn't a thing.

(Technically, "pattern matching" encompasses both dispatching on sums and destructuring assignment on products, and to be fair, not a lot of imperative languages have good support for the latter either. But many do!)

Re: Functional Language Features: Iterators and Closures

#8
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

Rust enums aren't like C style enums. They are a kind of algebraic data type: a sum type. Sum types haven't, until recently, been very well represented outside of functional programming languages. In haskell for example you might have a function that accepts a sum type, and to handle it you pattern match each variant and provide an implementation for that variant.

This is pretty core to functional programming languages like haskell, and its very similar to how rust behaves, and is omnipresent much like in haskell. For example, rust does not have exceptions, but repersents errors via the `Result` enum which is analogous to Haskell's `Either` data type. In order to access any value wrapped in an Result you must pattern match on it and explictly handle the possibility of an error.

All of this comes from functional programming languages, but they are starting to become common place in mainstream languages.

Re: Functional Language Features: Iterators and Closures

#9
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

In general the article is woefully imprecise. Yes, Rust does support higher order functions and other aspects of functional programming, but closures can have side effects. There is no concept of referential integrity in the language (I wish there was something like a “pure” keyword). Don’t get me wrong, I love Rust, but in the scheme of things, it’s a very imperative language.

Re: Functional Language Features: Iterators and Closures

#10
post #3
post #2

> Programming in a functional style often includes using functions as values by passing them in arguments, returning them from other functions, assigning them to variables for later execution, and so forth. > ... > Other Rust features, such as pattern matching and enums, which we’ve covered in other chapters, are influenced by the functional style as well. What is it about pattern matching and enums that associates t…

I believe, on the pattern matching side, it has to do with the fact that it's a higher level feature that feels much more declarative. Inside of writing a sequence of instructions to check whether a list is 3 and the first two are equal, one writes the declarative syntax: (define (f x) (match x [`(,x ,x ,y) y] [_ #f]))

I'm sure it makes sense to someone well-versed in Rust syntax, but to an outsider like me that code looks like the kind of "line noise" that made me switch from perl to python.

The procedural 'if x.len == 3 and x[0] == x[1]' seems a lot clearer as to what is actually being tested.

Post reply on HN