(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
How to add imperative programming to a pure functional language | |
=== | |
Many people bemoan languages such as Haskell for not supporting imperative | |
programming; they decry the need for math in their computer science. | |
 | |
I'm here to tell you that not only does Haskell make imperative programming a | |
cinch, but safe and correct as well. Follow along! This post is written in |
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
class Circle | |
constructor: (@x, @y, @radius, @x_move, @y_move) -> | |
class Overlap | |
constructor: (@x = 0, @y = 0, @amount = 0) -> | |
mysketch = (sketch) -> | |
circles = [] |
(ns quil-js.circles | |
(:require [quil.core :as q :include-macros true] | |
[quil.middleware :as m])) | |
(deftype Circle [x y radius x-move y-move line-color fill-color alpha]) | |
(defn draw-circle [circle] | |
; (.log js/console (str "Drawing " x "," y " with " overlap)) |
This gist had a far larger impact than I imagined it would, and apparently people are still finding it, so a quick update:
(async main(){...}())
as a substitute for TLA. This completely eliminates the blocking problem (yay!) but it's less powerful, and harder to statically analyse (boo). In other words the lack of TLA is causing real problemsI'll leave the rest of this document unedited, for archaeological
/*** | |
You've probably seen FizzBuzz before. If you need a reminder, the problem is | |
this: Print out a list of numbers from 1 to 100, except that if a number's | |
divisible by 3, replace it with "Fizz", and if it's divisible by 5, replace it | |
with "Buzz". If it's divisible by 3 and 5, replace it with "FizzBuzz". | |
This is pretty straightforward to do in JavaScript with a for loop and some | |
if-then blocks, but it quickly gets more complicated. What if we want to add | |
"Quux" at the end of every number divisible by 7? And also add "Prime" at the |
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