Skip to content
This repository
Browse code

add Enumerable#drop_last:

  • Loading branch information...
commit 9ff3195755486d5c76b56246546a3ffa3a1501b3 1 parent f603802
Christopher Bertels authored

Showing 2 changed files with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. +12 0 lib/enumerable.fy
  2. +19 0 tests/string.fy
12 lib/enumerable.fy
@@ -504,6 +504,18 @@ class Fancy {
504 504
505 505 alias_method: 'skip: for: 'drop:
506 506
  507 + def drop_last: amount (1) {
  508 + """
  509 + @amount Amount of elements to drop from the end.
  510 + @return New @Array@ without last @amount elements.
  511 +
  512 + Example:
  513 + [1,2,3,4] drop_last: 2 # => [3,4]
  514 + """
  515 +
  516 + first: (size - amount)
  517 + }
  518 +
507 519 def reduce: block init_val: init_val {
508 520 """
509 521 Calculates a value based on a given block to be called on an accumulator
19 tests/string.fy
@@ -55,6 +55,25 @@ FancySpec describe: String with: {
55 55 "hello world" drop_while: |c| { c != " " } . join: "" . is: " world"
56 56 }
57 57
  58 + it: "drops the last element" with: 'drop_last: when: {
  59 + [] drop_last is: []
  60 + [1] drop_last is: []
  61 + "foo" drop_last is: ["f", "o"]
  62 + (1,2,3) drop_last is: [1,2]
  63 + }
  64 +
  65 + it: "rops the last n elements" with: 'drop_last: when: {
  66 + [] drop_last: 1 . is: []
  67 + [] drop_last: 2 . is: []
  68 + [] drop_last: 0 . is: []
  69 + [1] drop_last: 0 . is: [1]
  70 + [1] drop_last: 1 . is: []
  71 + [1] drop_last: 2 . is: []
  72 + [1,2,3] drop_last: 1 . is: [1, 2]
  73 + [1,2,3] drop_last: 2 . is: [1]
  74 + [1,2,3] drop_last: 3 . is: []
  75 + }
  76 +
58 77 it: "is empty" with: 'empty? when: {
59 78 "" empty? is: true
60 79 " " empty? is: false

0 comments on commit 9ff3195

Please sign in to comment.
Something went wrong with that request. Please try again.