Skip to content

Instantly share code, notes, and snippets.

@cscalfani
cscalfani / FuncApplicationElm.md
Last active June 11, 2018 22:59
Fun with Function Application (Elm version)

Fun with Function Application (Elm version)

Function application operators in Elm make programming more understandable and help reduce parenthesis.

(<|)

The application operator, <|, can be used to reduce the need for parenthesis. The following:

@cscalfani
cscalfani / FuncApplicationHaskell.md
Created June 9, 2018 18:44
Fun with Function Application (Haskell version)

Fun with Function Application (Haskell version)

Function application operators in Haskell make programming more understandable and help reduce parenthesis.

($)

The application operator, $, can be used to reduce the need for parenthesis. The following:

@cscalfani
cscalfani / ThinkAboutMonads.md
Last active December 4, 2022 20:58
How to think about monads

How to think about Monads

UPDATE 2021: I wrote this long before I wrote my book Functional Programming Made Easier: A Step-by-step Guide. For a much more in depth discussion on Monads see Chapter 18.

Initially, Monads are the biggest, scariest thing about Functional Programming and especially Haskell. I've used monads for quite some time now, but I didn't have a very good model for what they really are. I read Philip Wadler's paper Monads for functional programming and I still didnt quite see the pattern.

It wasn't until I read the blog post You Could Have Invented Monads! (And Maybe You Already Have.) that I started to see things more clearly.

This is a distillation of those works and most likely an oversimplification in an attempt to make things easier to understand. Nuance can come later. What we need when first le

@cscalfani
cscalfani / mapAllWrong.md
Last active April 28, 2021 09:33
You're thinking about map all wrong

(Compose) Compose (Compose)

Motivation

Multiple times I've wanted to understand how to derive the following by hand:

(.) . (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d

Which is the point free version of:

@cscalfani
cscalfani / DiscoveringApplicative.md
Created December 27, 2017 02:23
Discovering Applicative Functors in Haskell

Discovering Applicative Functors in Haskell

When first learning Applicatives, they always seemed an oddball thing to me. At first, Functors made sense and Monads sort of made sense, but Applicatives felt shoehorned in between the two.

That is until I was reading Programming In Haskell by Graham Hutton, Second Edition (section 12.2) on Applicatives. His explanation takes a bit of a leap and is presented in an odd order, but I think I can reorder the information in such a way that we can feel as if we are discovering Applicative Functors (or Applicatives for short) ourselves.

Start with Functor

Let's remind ourselves of the Functor class definition:

Monads of Functions in Haskell

We're all pretty familiar with Monads that contain values, e.g. Maybe a and Either e a. Typically, these are value type, e.g. Maybe String, Maybe Int, Either String Int, Either MyError MyResult.

When fmap is called on a Maybe Int, it is clear that the Int is going to be mapped to its new value when the fmap is evaluated, e.g.:

fmap (* 10) Just 1 === Just 10
@cscalfani
cscalfani / CompositionWithMultipleParameters.md
Created December 5, 2017 22:29
Functional Composition with Multiple Parameters in Haskell

Functional Composition with Multiple Parameters in Haskell

In the past, I've written composition functions in both Elm and Haskell that take multiple parameters for the leftmost function, i.e. the function that gets applied first.

(All examples here are in Haskell)

Here was my Haskell implemenation (stolen from the web):

compose2 :: (c -&gt; d) -&gt; (a -&gt; b -&gt; c) -&gt; a -&gt; b -&gt; d
@cscalfani
cscalfani / MonoidsInHaskellAnIntroductions.md
Last active November 14, 2023 09:30
Monoids in Haskell, an Introduction

Monoids in Haskell, an Introduction

Why should programmers care about Monoids? Because Monoids are a common pattern that shows up over and over in programming. And when patterns show up, we can abstract them and leverage work we've done in the past. This allows us to quickly develop solutions on top of proven, stable code.

Add Commutative Property to a Monoid (Commutative Monoid) and you have something that can be executed in parallel. With the end of Moore's Law, parallelism is our only hope to increasing processing speeds.

What follows is what I've learned after studying Monoids. It is hardly complete, but hopefully will prove to be helpful as an introduction for others.

Monoid Lineage

@cscalfani
cscalfani / coContraFunctors.md
Last active January 4, 2021 20:47
Covariant and Contravariant Functors

Covariant and Contravariant Functors

This article contains more than enough examples to hopefully understand Covariant and Contravariant Functors and how to create them for many situtations.

Note that all type annotations use Int as the concrete type but any concrete type would be equivalent as would any mix of concrete types.

It assumes a general understanding of Functor and a basic understanding of Haskell type classes and data types.

Mapping from a to b(#1)