-
-
Save hossameldeen/9ead5f6b4b92a6d4b821 to your computer and use it in GitHub Desktop.
Forking Paths in Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Initial code: https://gist.github.com/dela3499/2b70e5df7a387430adad | |
import Graphics.Element exposing (show) | |
import List | |
import Regex | |
import Dict exposing (Dict, foldl) | |
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 = | |
foldl replaceString string 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) | |
{-- | |
Last refactor made: | |
- There's `foldl` in Dict. | |
- Using static import with Dict.foldl | |
--} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment