Skip to content

Instantly share code, notes, and snippets.

@dela3499
Last active August 29, 2015 14:25
Show Gist options
  • Save dela3499/0212bc84e92f0bc5430e to your computer and use it in GitHub Desktop.
Save dela3499/0212bc84e92f0bc5430e to your computer and use it in GitHub Desktop.
Succinctly expressing random number generation in a functional language
-- Original code to generate random numbers
let (nChars, seed2) = generate (int 3 5) seed1
(vIndices, seed3) = generate (list nChars (int 0 1000)) seed2
(cIndices, seed4) = generate (list nChars (int 0 1000)) seed3
(length, seed5) = generate (int 0 2) seed4
config = {vIndices = vIndices,
cIndices = cIndices}
-- Dictionary that represents computation
-- Same format found in http://dask.pydata.org/en/latest/spec.html
{'nChars': (int,3,5),
'vIndices': (list,'nChars',(int,0,1000)),
'cIndices': (list,'nChars',(int,0,1000)),
'length': (int,0,2)}
@dela3499
Copy link
Author

The original code has a lot of boilerplate to pass around the seed and to execute the generator.
Compare them:

(vIndices, seed3) = generate (list nChars (int 0 1000)) seed2
'vIndices': (list,'nChars',(int,0,1000))

Every call into the original code had to:

  1. Assign a new seed
  2. Specify the input seed
  3. Call the generate function

These can all be abstracted away.

@dela3499
Copy link
Author

The key insight from dask is that you can specify the graph of dependencies very simply with a dictionary of tuples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment