Skip to content

Instantly share code, notes, and snippets.

@amscotti
Created July 13, 2018 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amscotti/a51e1a34f085623678b6eba5c017d4ec to your computer and use it in GitHub Desktop.
Save amscotti/a51e1a34f085623678b6eba5c017d4ec to your computer and use it in GitHub Desktop.
filterIt not working
import unittest
import algorithm, sequtils
type
Student* = tuple[name: string, grade: int]
School* = object
students*: seq[Student]
proc roster*(s: School): seq[string] =
s.students
.sortedByIt((it.grade, it.name))
.mapIt(it.name)
proc grade*(s: School, g: int): seq[string] =
s.students
.filter(proc(i: Student): bool = i.grade == g)
.sortedByIt((it.grade, it.name))
.mapIt(it.name)
# Doesn't work - s.students.filterIt(it.grade == g).sortedByIt((it.grade, it.name)).mapIt(it.name)
# Given students' names along with the grade that they are in,
# create a roster for the school.
suite "Grade School":
test "Adding a student adds them to the sorted roster":
let school = School(students: @[("Aimee", 2)])
check school.roster() == @["Aimee"]
test "Adding more student adds them to the sorted roster":
let school = School(students: @[("Blair", 2), ("James", 2), ("Paul", 2)])
check school.roster() == @["Blair", "James", "Paul"]
test "Adding students to different grades adds them to the same sorted roster":
let school = School(students: @[("Chelsea", 3), ("Logan", 7)])
check school.roster() == @["Chelsea", "Logan"]
test "Roster returns an empty list if there are no students enrolled":
let school = School(students: @[])
let expected: seq[string] = @[]
check school.roster() == expected
test "Student names with grades are displayed in the same sorted roster":
let school = School(students: @[
("Peter", 2),
("Anna", 1),
("Barb", 1),
("Zoe", 2),
("Alex", 2),
("Jim", 3),
("Charlie", 1)
])
check school.roster() == @["Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"]
test "Grade returns the students in that grade in alphabetical order":
let school = School(students: @[("Franklin", 5), ("Bradley", 5), ("Jeff", 1)])
check school.grade(5) == @["Bradley", "Franklin"]
test "Grade returns an empty list if there are no students in that grade":
let school = School(students: @[])
let expected: seq[string] = @[]
check school.grade(1) == expected
@amscotti
Copy link
Author

Logs from the build,

nimble build --verbose
Setting Nim stdlib prefix to
Setting Nim stdlib path to /Users/ascotti/.choosenim/toolchains/nim-0.18.0/lib
Verifying dependencies for grade_school@0.1.0
Building grade_school/grade_school using c backend
Error: Build failed for package: grade_school
... Details:
... Execution failed with exit code 1
... Command: "/Users/ascotti/.nimble/bin/nim" c --noBabelPath -o:"/Users/ascotti/Documents/code/nim/grade_school/grade_school" "/Users/ascotti/Documents/code/nim/grade_school/src/grade_school.nim"
... Output: Hint: used config file '/Users/ascotti/.choosenim/toolchains/nim-0.18.0/config/nim.cfg' [Conf]
... Hint: system [Processing]
... Hint: grade_school [Processing]
... Hint: unittest [Processing]
... Hint: macros [Processing]
... Hint: strutils [Processing]
... Hint: parseutils [Processing]
... Hint: math [Processing]
... Hint: algorithm [Processing]
... Hint: streams [Processing]
... Hint: times [Processing]
... Hint: posix [Processing]
... Hint: sets [Processing]
... Hint: hashes [Processing]
... Hint: os [Processing]
... Hint: ospaths [Processing]
... Hint: terminal [Processing]
... Hint: strformat [Processing]
... Hint: unicode [Processing]
... Hint: colors [Processing]
... Hint: tables [Processing]
... Hint: termios [Processing]
... Hint: sequtils [Processing]
... lib/pure/collections/sequtils.nim(398, 7) Error: internal error: environment misses: result

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