Skip to content

Instantly share code, notes, and snippets.

@dela3499
Last active March 6, 2016 22:27
Show Gist options
  • Save dela3499/2b70e5df7a387430adad to your computer and use it in GitHub Desktop.
Save dela3499/2b70e5df7a387430adad to your computer and use it in GitHub Desktop.
Forking Paths in Elm
import Graphics.Element exposing (show)
import List
import Regex
import Dict exposing (Dict)
import String
import Random
import Array
import Set
-- Return list with no duplicate elements
unique x = Set.toList (Set.fromList x)
-- Run find and replace on a string
replaceString: (String, String) -> String -> String
replaceString (search, replacement) string =
Regex.replace Regex.All (Regex.regex search) (\_ -> replacement) string
-- Run find and replace on a string multiple times
replaceAll: String -> Dict String String -> String
replaceAll string replacements =
List.foldl replaceString string (Dict.toList replacements)
-- Randomly select one string from list
sample: List String -> Random.Seed -> (String, Random.Seed)
sample strings seed =
let n = List.length strings
(i, seed') = Random.generate (Random.int 0 (n - 1)) seed
sample = Array.get i (Array.fromList strings)
in case sample of
Just string -> (string, seed')
Nothing -> ("", seed')
pickOne : List (Generator String) -> Generator String
pickOne xs =
let
get ys i =
case ys of
z :: zs -> if i == 0 then z else get zs (i - 1)
[] -> constant ""
in
Random.andThen (Random.int 0 (List.length xs - 1)) (get xs)
-- Run find and replace, selecting a replacement randomly from list of strings
replaceStringRandomly: (String, List String) -> Generator String -> Generator String
replaceStringRandomly (search, replacements) stringGen =
let replacementGen = pickOne (List.map constant replacements)
replaceString' replacement = Random.andThen stringGen replaceString (search, replacement)
in Random.andThen replacementGen replaceString'
-- Run find and replace on multiple search strings, and randomly select replacements from list
replaceAllRandomly: String -> Dict String (List String) -> Generator String
replaceAllRandomly string replacements =
List.foldl replaceStringRandomly (constant string) (Dict.toList replacements)
-- Run find and replace on multiple search screens multiple times for a given string
replaceAllRandomly100: String -> Dict String (List String) -> Generator String
replaceAllRandomly100 string replacements =
List.foldl (\_ newString -> (replaceAllRandomly newString replacements) seed) (string, seed) [1..100]
repeatHelper: (Random.Seed -> (a, Random.Seed)) -> Int -> (Random.Seed, List a) -> (Random.Seed, List a)
repeatHelper f i (seed, values) =
let (value, seed') = f seed
in (seed', value :: values)
-- Run full find and replace process to generate many output strings with different random choices
repeatRandomly: (Random.Seed -> (a, Random.Seed)) -> Int -> Random.Seed -> List a
repeatRandomly f n seed =
List.foldl (repeatHelper f) (seed, []) [1..n] |> snd
-- Get some number of string substitutions
getSubs: String -> Dict String (List String) -> Generator String
getSubs string substitutions =
let subtitute = replaceAllRandomly100 string substitutions
in repeatRandomly subtitute n seed |> unique
------------------------------------------------------------
seed = Random.initialSeed 5232334
string = "We should #verb #preposition #place."
substitutions = Dict.fromList
[ ("#place", [ "my house", "your house", "#country club"])
, ("#country club", [ "Wright Park", "the Oak Creek #OCplace"])
, ("#OCplace", [ "bar", "golf course", "pool"])
, ("#preposition", [ "to", "toward", "under", "through", "onto", "into"])
, ("#verb", [ "go", "escape", "travel"])
]
-- Generate 100 strings with randomly chosen replacements, and remove duplicates
main = show (getSubs string substitutions 100 seed)
import Graphics.Element exposing (show)
import List
import Regex
import Dict exposing (Dict)
import String
import Random
import Array
import Set
-- Return list with no duplicate elements
unique x = Set.toList (Set.fromList x)
-- Run find and replace on a string
replaceString: (String, String) -> String -> String
replaceString (search, replacement) string =
Regex.replace Regex.All (Regex.regex search) (\_ -> replacement) string
-- Run find and replace on a string multiple times
replaceAll: String -> Dict String String -> String
replaceAll string replacements =
List.foldl replaceString string (Dict.toList replacements)
-- Randomly select one string from list
sample: List String -> Random.Seed -> (String, Random.Seed)
sample strings seed =
let n = List.length strings
(i, seed') = Random.generate (Random.int 0 (n - 1)) seed
sample = Array.get i (Array.fromList strings)
in case sample of
Just string -> (string, seed')
Nothing -> ("", seed')
-- Run find and replace, selecting a replacement randomly from list of strings
replaceStringRandomly: (String, List String) -> (String, Random.Seed) -> (String, Random.Seed)
replaceStringRandomly (search, replacements) (string, seed) =
let (replacement, seed') = sample replacements seed
in (replaceString (search, replacement) string, seed')
-- Run find and replace on multiple search strings, and randomly select replacements from list
replaceAllRandomly: String -> Dict String (List String) -> Random.Seed -> (String, Random.Seed)
replaceAllRandomly string replacements seed =
List.foldl replaceStringRandomly (string, seed) (Dict.toList replacements)
-- Run find and replace on multiple search screens multiple times for a given string
replaceAllRandomly100: String -> Dict String (List String) -> Random.Seed -> (String, Random.Seed)
replaceAllRandomly100 string replacements seed =
List.foldl (\_ (newString, seed) -> (replaceAllRandomly newString replacements) seed) (string, seed) [1..100]
repeatHelper: (Random.Seed -> (a, Random.Seed)) -> Int -> (Random.Seed, List a) -> (Random.Seed, List a)
repeatHelper f i (seed, values) =
let (value, seed') = f seed
in (seed', value :: values)
-- Run full find and replace process to generate many output strings with different random choices
repeatRandomly: (Random.Seed -> (a, Random.Seed)) -> Int -> Random.Seed -> List a
repeatRandomly f n seed =
List.foldl (repeatHelper f) (seed, []) [1..n] |> snd
-- Get some number of string substitutions
getSubs: String -> Dict String (List String) -> Int -> Random.Seed -> List String
getSubs string substitutions n seed =
let subtitute = replaceAllRandomly100 string substitutions
in repeatRandomly subtitute n seed |> unique
------------------------------------------------------------
seed = Random.initialSeed 5232334
string = "We should #verb #preposition #place."
substitutions = Dict.fromList
[ ("#place", [ "my house", "your house", "#country club"])
, ("#country club", [ "Wright Park", "the Oak Creek #OCplace"])
, ("#OCplace", [ "bar", "golf course", "pool"])
, ("#preposition", [ "to", "toward", "under", "through", "onto", "into"])
, ("#verb", [ "go", "escape", "travel"])
]
-- Generate 100 strings with randomly chosen replacements, and remove duplicates
main = show (getSubs string substitutions 100 seed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment