Skip to content

Instantly share code, notes, and snippets.

View shkesar's full-sized avatar

shubham shkesar

View GitHub Profile

The thing that students have the hardest time on when learning functional programming is how to process a recursive structure while maintaining some sort of "state", the result if you will. I'll attempt here to demystify the process.

Functional programming languages almost always use a lot of recursively defined structures. Depending on the language those can be implemented in various ways, but in any case the end result is the same. A structure of this type is either an "atom", i.e. an irreducible thing, or a "compound" consisting of substructures of the same form.

For example a "list" is either an Empty/Nil list (the "atom") or it is formed as a Cons of a value and another list (compound form). That other "sublist" can itself be empty or another cons and so on and so forth. A tree is similar. It is either empty, or it consists of a triple of a value and two sub-trees, left and right.

Almost every problem we encounter is a question about doing something with all entries in a structure. To solve these prob

@staltz
staltz / introrx.md
Last active September 20, 2024 10:10
The introduction to Reactive Programming you've been missing
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
__all__ = ["transform"]
__version__ = '0.3'
__author__ = 'Christoph Burgmer <cburgmer@ira.uka.de>'
__url__ = 'http://github.com/cburgmer/upsidedown'
@l0gicpath
l0gicpath / all_users.csv
Last active November 11, 2017 13:59
Subtract data of one CSV file from another using fast grep. Dummy data generated by http://dummydata.me/
jame@out.org Stefanie House
dolores.bernice@roofroom.me Dinah Erickson
luke.homer.gerard@smile.info Melisa Reynolds
salvador.perry@selectionself.info Lily Carson
rich.jamel@cover.me Millard Rubio
benita@rangerat.edu Octavia Santana
vickie.mattie@westwet.info Royal Bender
dwayne@part.me Laverne Hutchins
jeanette.laurie.katie@light.me Sherman Wilder
jami.elnora@smokesmooth.edu Thelma Burris
@blackwolf12333
blackwolf12333 / annotation command
Last active January 2, 2016 05:49
A prototype for how to use annotations to map the commands in a cleaner way.
@Command(command="save", aliases="s,sv")
public void save() {
player.save();
}
public void setup() {
list<Method> commands = getAllAnnotatedMethods();
for(Method m : commands) {
commandmap.put(m.getAnnotation().command(), m);
}
@ostronom
ostronom / CountryController.scala
Created February 20, 2012 17:00
Play 2.0 & squeryl simple integration
package controllers
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.{Session}
import play.api._
import play.api.mvc._
import models._
@rohitdholakia
rohitdholakia / Dictionary.py
Created December 27, 2011 21:22
A python script to generate a dictionary to be used with NaiveBayes
#This script reads all files in all directories of the folder taken from the openClassroom site and generates the dictionary, which we can then store in a file
folders = ["spam-train","spam-test","nonspam-train","nonspam-test"]
import os,sys
#We need a dictionary to store word occurences. What we can do is create a default dict and then update the frequencies. Write it all into a file all at once.
from collections import *
dictionary = defaultdict(int)
fdict = open(sys.argv[2],'w') #File to write all the entries in the dictionary
for root,dirnames,filenames in os.walk(sys.argv[1]):
for d in dirnames: #For each directory
for f in os.listdir(d):