dev-resources.site
for different kinds of informations.
Scope progression
Published at
10/18/2024
Categories
functional
fsharp
go
Author
lamg
Main Article
Author
4 person written this
lamg
open
In imperative programming, we usually have code that looks the following way:
func addOneToSlice(xs []int) []int {
rs := make([]int, len(xs))
for i, value := range xs {
rs[i] = value + 1
}
return rs
}
However, notice the following about the for
loop:
- Each iteration has a specific purpose, which is to add one to the current element.
- However, each iteration has no constraint on which element it can operate.
- Operating with
xs[i+2]
andrs[i+3]
wouldn't fundamentally alter the structure of the code we have, while making the end result incorrect.
Compare how the same task would be done in F#:
let rec addOneToList =
function
| [] -> []
| x :: xs -> x + 1 :: addOneToList xs
Now consider the following:
- We have a list as a function argument.
- A list in functional languages is a linked list.
- The efficient and standard operations on linked lists are:
- Separating the head
x
from its tailxs
- Doing something to the head
x
- Comparing the list passed as a parameter with the empty list
[]
- Separating the head
Given these restrictions, adding 1
to any element y
not at the head of the list would significantly alter the structure of our function.
Now compare how the computation progresses in both styles:
- In the functional style, we create a new scope with new values, which involves making a recursive call in the example above.
- In the imperative style, we mutate an existing value without changing the scope.
In functional style, marrying both scope with computational progress has the following consequences:
- We avoid mutation.
- The execution flow is explicit.
- The structure we are dealing with becomes clear.
functional Article's
30 articles in total
A monad is a monoid in the category of endofunctors...
read article
Rust-like Iteration in Lua
read article
Transducer: A powerful function composition pattern
read article
🏗️ `Is` Methods
read article
7.bet’s Bold Move: Play Smarter, Play Safer, Play Better!
read article
Harnessing the Power of Object-Oriented and Functional Programming Paradigms in Software Development
read article
Lambda vs. Named Functions: Choosing the Right Tool for the Job
read article
Object-Oriented vs Functional Programming—Why Not Both?
read article
From C# to Haskell and Back Again: My Journey into Functional Programming
read article
Comprehensive Guide to Automated Functional Testing
read article
Functional Programming in Go with IBM fp-go: Error Handling Made Explicit
read article
Razumevanje funkcija višeg reda (Higher-Order Functions) u JavaScript-u
read article
What is Functional Programming, and How Can You Do It in JavaScript?
read article
Parallel Testing: Best Practice for Load Testing & Functional Testing
read article
For loops and comprehensions in Elixir - transforming imperative code
read article
Advent of Code and Aesthetics
read article
PureScript for Scala developers
read article
Clojure REPL-Driven Development with VS Code
read article
Combining Object-Oriented and Functional Programming in Large Projects
read article
Non-Functional Requirements: A Comprehensive Guide
read article
Unpacking Lambda Expressions: What They Are and Why They Matter
read article
Functional Programming: A Misfit for Backend Engineering
read article
Scope progression
currently reading
JavaScript Functions for Beginners: Quick Guide
read article
Tech Watch #2
read article
Either Algebraic Data Type
read article
Functional Programming in C#: The Practical Parts
read article
A 20-liner Drag'n'Drop feat using ObservableTypes
read article
On “superiority” of (functional) programming and other labels
read article
Top Open Source functional programming technologies
read article
Featured ones: