Skip to content

Instantly share code, notes, and snippets.

@gregberns
Last active November 29, 2018 07:21
Show Gist options
  • Save gregberns/70dba7a3b4fb8991928a73d9ef8be916 to your computer and use it in GitHub Desktop.
Save gregberns/70dba7a3b4fb8991928a73d9ef8be916 to your computer and use it in GitHub Desktop.
List a -> Int -> Int -> List (List a)
//How can we do this imperitive process with Haskell/Purescript style functions?
//Examples
// > generateQuestions([1,2,3,4,5,6,7,8])(3)(2)
// [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
// > generateQuestions([1,2,3,4,5])(2)(4)
// [ [ 1, 2 ], [ 3, 4 ], [ 5, 1 ], [ 2, 3 ] ]
// Imperitive Code
// generateQuestions :: List Question -> Int -> Int -> List (List Question)
const generateQuestions = questions => questionsPerReference => countOfReferences => {
var outter = []
var m = 0
for (var i = 0; i < countOfReferences; i++) {
var inner = []
for (var j = 0; j < questionsPerReference; j++) {
inner.push(questions[m])
if (m >= questions.length - 1) {
m = 0
} else {
m++
}
}
outter.push(inner)
}
return outter
}
@gregberns
Copy link
Author

from peddie on irc
generateQuestions questions qPR cOR = take cOR . group qPR $ cycle questions
where group comes from:
https://stackoverflow.com/questions/12876384/grouping-a-list-into-lists-of-n-elements-in-haskell

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