diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..71f09ae --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +on: [push, pull_request] +name: CI +jobs: + build: + name: "Build on Racket '${{ matrix.racket-version }}' (${{ matrix.racket-variant }})" + runs-on: ubuntu-latest + continue-on-error: ${{ matrix.experimental || false }} + strategy: + fail-fast: false + matrix: + racket-version: ["stable", "current"] + racket-variant: ["BC", "CS"] + include: + - racket-version: current + experimental: true + steps: + - uses: actions/checkout@v3.1.0 + - uses: Bogdanp/setup-racket@v1.9.1 + with: + architecture: x64 + distribution: full + variant: ${{ matrix.racket-variant }} + version: ${{ matrix.racket-version }} + - name: Installing monadicrkt and its dependencies + run: raco pkg install --no-docs --auto --name monadicrkt + - name: Compiling monadicrkt and building its docs + run: raco setup --check-pkg-deps --unused-pkg-deps monadicrkt + - name: Testing monadicrkt + run: raco test -x -p monadicrkt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a59348 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*~ +\#* +.\#* +.DS_Store +compiled/ +/doc/ diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..c819e08 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,23 @@ +monadicrkt + +MIT License + +Copyright (c) 2023 hdbian + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb3af9e --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +monadicrkt +========== + +monadic containers for Racket + + +## Usage + +```racket +(require monadicrkt) + +(define foo (make-nothing)) + +``` \ No newline at end of file diff --git a/info.rkt b/info.rkt new file mode 100644 index 0000000..6b4173a --- /dev/null +++ b/info.rkt @@ -0,0 +1,9 @@ +#lang info +(define collection "monadicrkt") +(define deps '("base")) +(define build-deps '("scribble-lib" "racket-doc" "rackunit-lib")) +(define scribblings '(("scribblings/monadicrkt.scrbl" ()))) +(define pkg-desc "Description Here") +(define version "0.0") +(define pkg-authors '(dbian)) +(define license '(MIT)) diff --git a/main.rkt b/main.rkt new file mode 100644 index 0000000..7f3d01b --- /dev/null +++ b/main.rkt @@ -0,0 +1,55 @@ +#lang racket/base + +(require "src/result.rkt") +(require "src/maybe.rkt") + +(provide (all-from-out "src/result.rkt" "src/maybe.rkt")) + +(module+ test + (require rackunit)) + +;; Notice +;; To install (from within the package directory): +;; $ raco pkg install +;; To install (once uploaded to pkgs.racket-lang.org): +;; $ raco pkg install <> +;; To uninstall: +;; $ raco pkg remove <> +;; To view documentation: +;; $ raco docs <> +;; +;; For your convenience, we have included LICENSE-MIT and LICENSE-APACHE files. +;; If you would prefer to use a different license, replace those files with the +;; desired license. +;; +;; Some users like to add a `private/` directory, place auxiliary files there, +;; and require them in `main.rkt`. +;; +;; See the current version of the racket style guide here: +;; http://docs.racket-lang.org/style/index.html + +;; Code here + + + +(module+ test + ;; Any code in this `test` submodule runs when this file is run using DrRacket + ;; or with `raco test`. The code here does not run when this file is + ;; required by another module. + + (check-equal? (+ 2 2) 4)) + +(module+ main + ;; (Optional) main submodule. Put code here if you need it to be executed when + ;; this file is run using DrRacket or the `racket` executable. The code here + ;; does not run when this file is required by another module. Documentation: + ;; http://docs.racket-lang.org/guide/Module_Syntax.html#%28part._main-and-test%29 + + (require racket/cmdline) + (define who (box "world")) + (command-line + #:program "my-program" + #:once-each + [("-n" "--name") name "Who to say hello to" (set-box! who name)] + #:args () + (printf "hello ~a~n" (unbox who)))) diff --git a/scribblings/monadicrkt.scrbl b/scribblings/monadicrkt.scrbl new file mode 100644 index 0000000..250cabb --- /dev/null +++ b/scribblings/monadicrkt.scrbl @@ -0,0 +1,10 @@ +#lang scribble/manual +@require[@for-label[monadicrkt + racket/base]] + +@title{monadicrkt} +@author{hdbian} + +@defmodule[monadicrkt] + +Package Description Here diff --git a/src/maybe.rkt b/src/maybe.rkt new file mode 100644 index 0000000..4d1c87a --- /dev/null +++ b/src/maybe.rkt @@ -0,0 +1,30 @@ +#lang racket +(provide (all-defined-out)) + +(struct maybe-type-just [value]) +(struct maybe-type-nothing []) + +(define (make-just value) + (maybe-type-just value)) + +(define (make-nothing) + (maybe-type-nothing)) + +(define (maybe-type? x) + (or (maybe-type-just? x) + (maybe-type-nothing? x))) + +(define (maybe-map maybe f) + (cond ((maybe-type-just? maybe) + (make-just (f (maybe-type-just-value maybe)))) + ((maybe-type-nothing? maybe) + maybe))) + +(define (maybe-bind maybe f) + (cond ((maybe-type-just? maybe) + (f (maybe-type-just-value maybe))) + ((maybe-type-nothing? maybe) + maybe))) + +(define (maybe-just-value maybe) + (maybe-type-just-value maybe)) \ No newline at end of file diff --git a/src/result.rkt b/src/result.rkt new file mode 100644 index 0000000..e40c1ca --- /dev/null +++ b/src/result.rkt @@ -0,0 +1,41 @@ + +#lang racket +(require "maybe.rkt") +(struct result-type-success [value]) +(struct result-type-error [message]) + +(define (make-success value) + (result-type-success value)) + +(define (make-error message) + (result-type-error message)) + +(define (result-type? x) + (or (result-type-success? x) + (result-type-error? x))) + +(define (result-map result f) + (cond ((result-type-success? result) + (make-success (f (result-type-success-value result)))) + ((result-type-error? result) + result))) + +(define (result-bind result f) + (cond ((result-type-success? result) + (f (result-type-success-value result))) + ((result-type-error? result) + result))) + +(define (result2maybe result) + (cond ((result-type-success? result) + (make-just (result-type-success-value result))) + ((result-type-error? result) + (make-nothing)))) + +(define (result-success-val result) + (result-type-success-value result)) + +(define (result-error-val result) + (result-type-error-message result)) + +(provide (all-defined-out))