Skip to content

stalexxx/sekuence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

sekuence

Sequence-based random generator for kotlin. Inspired by Clojure test.check

Examples

Random of object list

fun languages(): Sequence<String> = of("clojure", "haskell", "erlang", "scala", "python")

//[ scala kotlin python clojure erlang ]

Combination with probabilities

fun mostlyInts(): Sequence<Any> = frequency(9 to ints(), 1 to ret(1))

Positive and Even

fun evenAndPositive(): Sequence<Int> = posInt().fmap { it * 2 }

//

Power of two

fun powerOfTwo(): Sequence<Int> = posInt().fmap { Math.pow(2.0, it.toDouble()).toInt() }

Sequence of sorted ints lists

fun sortedLists(): Sequence<List<Int>> = genList(ints()).fmap { it.sorted() }

Pair example

fun intAndBoolean(): Sequence<Pair<Int, Boolean>> = genPair(ints(), bool())

Anything by five

fun anythingButFive(): Sequence<Int> = ints().except { it == 5 }

Binding

fun vectorAndElem(): Sequence<Pair<List<Int>, Int>> =
    genList(ints().except { it == 13 })
        .bind { it to it.randElement() }
        

Binding over type

data class User(var id: Int, var login: String, var email: String)
fun genUsers(): Sequence<User> = bind(ints(1..100), strings(), emails(), ::User)

Another way

data class User(var id: Int, var login: String, var email: String)
fun genUsers() = bind<User>(ints(1..100), names().fmap(String::toLowerCase), emails(strings())

returns

[ User(id=100, login=john, email=[email protected]) User(id=60, login=hual, email=[email protected]) User(id=56, login=john, email=[email protected]) User(id=69, login=john, email=[email protected]) User(id=45, login=john, email=[email protected]) ]

Releases

No releases published

Packages

No packages published

Languages