<commit_before>#lang racket/base
(provide
  maybe?
  (struct-out nothing)
  (struct-out just)
  maybe-filter
  maybe-fold
  maybe-from
  maybe-iterate
  maybe-map
  maybe-monad
  maybe-or
  )

(require
  "monad.rkt"
  "record.rkt"
  racket/function
  racket/match
  )

(module+ test
  (require
    rackunit
    ))

(records maybe
  (nothing)
  (just x))

(define (maybe-fold nothing-fold just-fold val)
  (match val
    ((nothing) nothing-fold)
    ((just x)  (just-fold x))))

(define (maybe-from default maybe-value)
  (maybe-fold default identity maybe-value))

(define (maybe-map f val)
  (maybe-fold val (compose1 just f) val))

(define (maybe-filter ms) (map just-x (filter just? ms)))

(module+ test
  (check-equal?
    (maybe-filter (list (nothing) (just 3) (nothing) (just 4)))
    '(3 4)
    ))

(define maybe-monad
  (monad
    just
    (lambda (prev next)
      (match prev
        ((nothing) (nothing))
        ((just x)  (next x))))))

(define-syntax maybe-or
  (syntax-rules ()
    ((_ arg ...)
     (cond
       ((let ((result arg))
          (if (just? result) result #f)) => (lambda (result) result))
       ...
       (else (nothing))))))

(define (maybe-iterate f arg)
  (match (f arg)
    ((nothing)  arg)
    ((just arg) (maybe-iterate f arg))))
<commit_msg>Define maybe-fold1<commit_after>#lang racket/base
(provide
  maybe?
  (struct-out nothing)
  (struct-out just)
  maybe-filter
  maybe-fold
  maybe-fold1
  maybe-from
  maybe-iterate
  maybe-map
  maybe-monad
  maybe-or
  )

(require
  "monad.rkt"
  "record.rkt"
  racket/function
  racket/match
  )

(module+ test
  (require
    rackunit
    ))

(records maybe
  (nothing)
  (just x))

(define (maybe-fold nothing-fold just-fold val)
  (match val
    ((nothing) nothing-fold)
    ((just x)  (just-fold x))))

(define (maybe-fold1 just-fold val) (maybe-fold val just-fold val))

(define (maybe-from default maybe-value)
  (maybe-fold default identity maybe-value))

(define (maybe-map f val) (maybe-fold1 (compose1 just f) val))

(define (maybe-filter ms) (map just-x (filter just? ms)))

(module+ test
  (check-equal?
    (maybe-filter (list (nothing) (just 3) (nothing) (just 4)))
    '(3 4)
    ))

(define maybe-monad
  (monad
    just
    (lambda (prev next)
      (match prev
        ((nothing) (nothing))
        ((just x)  (next x))))))

(define-syntax maybe-or
  (syntax-rules ()
    ((_ arg ...)
     (cond
       ((let ((result arg))
          (if (just? result) result #f)) => (lambda (result) result))
       ...
       (else (nothing))))))

(define (maybe-iterate f arg)
  (match (f arg)
    ((nothing)  arg)
    ((just arg) (maybe-iterate f arg))))

{'fraction_non_alphanumeric': 0.15966704502459325, 'fraction_numerical': 0.0052970109723798715, 'mean_word_length': 2.687587168758717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6215208', 'n_tokens_mistral': 994, 'n_tokens_neox': 952, 'n_words': 265}