Skip to content

Instantly share code, notes, and snippets.

@ppopoff
Last active January 7, 2017 19:11
Show Gist options
  • Save ppopoff/f82f27f3cf5284eef1282a7f58757bb8 to your computer and use it in GitHub Desktop.
Save ppopoff/f82f27f3cf5284eef1282a7f58757bb8 to your computer and use it in GitHub Desktop.
Sorting algorithms in mink
/** Module name should match the file name
* All functions in module are private by default
* to make an item public you should add '+' prior it's name
*
* All items in a module are represented as key-value pairs.
*/
/** An example of Collection type class
*/
+ type class Collection [A] = {
union: Collection[A] -> Collection[A] -> Collection[A]
intersect: Collection[A] -> Collection[A] -> Collection[A]
}
/** This function implements selection sort
* mink doesn't have explicit pattern matching construct
* you can pattern match over function signatures instead
*/
+ sort: List[T] -> List[T]: T < Ord =
Nil -> Nil
lst @ x :: xs ->
max = list.max
// a fact about lambdas
// you may think that it is a keyword, like it works for Kotlin
// or Groovy. It's wrong. You can use any symbolic name if it's not
// used in outer scope. It will be automatically bound to variable
allExceptMax = filter lst, it != max
sort allExceptMax ++ max
/** List.empty is an alias for Nil. I guess
* it's more sutable in the most cases
*/
+ qsort: List[T] -> List[T]: T < Ord =
List.empty -> Nil
pivot :: tail ->
lesser = tail.filter elem < pivot
larger = tail.filter elem >= pivot
qsort smaller ++ pivot ++ qsort larger
// will be predefined for Mink
Void = Unit -> Unit
+ sortCompare: Void = () -> {
// lists will be created by ctor call
sortedList = list 1, 2, 3, 4, 5
unsortedList = list 5, 3, 2, 1, 4
sortingWorks =
qsort unsortedList == sort unsortedList == sortedList
// a builtin if-then-else operator where:
// <boolean> ? <cond pos> ; <cond neg>
sortingWorks ? writeln "everything's great";
writeln "check your sorting implementation"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment