enumerable.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. pour(built_in_functions, {
  2. get_keys: fun (
  3. 'function get_keys (e: Enumerable) -> List',
  4. e => copy(enum_(e).get('keys'))
  5. ),
  6. get_values: fun (
  7. 'function get_values (e: Enumerable) -> List',
  8. e => copy(enum_(e).get('values'))
  9. ),
  10. get_entries: fun (
  11. 'function get_entries (e: Enumerable) -> List',
  12. e => list((function* () {
  13. let entry_list = enum_(e)
  14. let keys = entry_list.get('keys')
  15. let values = entry_list.get('values')
  16. assert(keys.length == values.length)
  17. let L = keys.length
  18. for (let i = 0; i < L; i += 1) {
  19. yield { key: keys[i], value: values[i] }
  20. }
  21. })())
  22. ),
  23. has: fun (
  24. 'function has (h: Hash, k: String) -> Bool',
  25. (h, k) => has(k, h)
  26. ),
  27. delete: fun (
  28. 'function delete (h: Hash, k: String) -> Void',
  29. (h, k) => {
  30. ensure(has(k, h), 'hash_invalid_delete')
  31. delete h[k]
  32. return Void
  33. }
  34. ),
  35. map_key: f (
  36. 'map_key',
  37. 'function map_key (h: Hash, f: Arity<1>) -> Hash',
  38. (h, f) => mapkey(h, k => call(f, [k])),
  39. 'function map_key (h: Hash, f: Arity<2>) -> Hash',
  40. (h, f) => mapkey(h, (k, v) => call(f, [k, v]))
  41. ),
  42. map_value: f (
  43. 'map_value',
  44. 'function map_value (h: Hash, f: Arity<1>) -> Hash',
  45. (h, f) => mapval(h, v => call(f, [v])),
  46. 'function map_value (h: Hash, f: Arity<2>) -> Hash',
  47. (h, f) => mapval(h, (v, k) => call(f, [v, k]))
  48. ),
  49. map_entry: fun (
  50. 'function map_entry (e: Enumerable, f: Arity<2>) -> Iterator',
  51. (e, f) => (function* () {
  52. let entry_list = enum_(e)
  53. let keys = entry_list.get('keys')
  54. let values = entry_list.get('values')
  55. assert(keys.length == values.length)
  56. let L = keys.length
  57. for (let i = 0; i < L; i += 1) {
  58. yield call(f, [keys[i], values[i]])
  59. }
  60. })()
  61. )
  62. })