class Map<K, V> {
...
V getOrSignal(K key) {
if containsKey(key) {
return get(key);
} else {
MissingKeySignal signal = throw new MissingKeyException(this, key);
return switch signal {
#lang racket | |
(require racket/generic) | |
(define-generics vable | |
(get-v vable)) | |
(struct a (v) | |
#:methods gen:vable | |
[(define (get-v a) |
// This is my attempt at translating the stream pipeline and transducer library I wrote for Racket | |
// into Effekt, using algebraic effects to model the actions of consuming and emitting values in a | |
// stream pipeline. The stream pipeline library I wrote is part of my package Rebellion, see its docs | |
// here: https://docs.racket-lang.org/rebellion/Streaming_Computations.html. The basics are that a | |
// stream pipeline combines an initial source (a lazy stream, sequence, or other similar object) and | |
// a terminal sink, called a *reducer*, along with any number of intermediate transformations called | |
// *transducers*. | |
type Option[A] { Present(value: A); Absent() } |
Mandatory APIs:
interface List {
size
add(x)
get(i)
set(i, x)
removeAt(i)
insert(i, x)
#lang racket/base | |
(require racket/contract/base) | |
(provide | |
(contract-out | |
[mutable-rb-tree-pict (-> mutable-rb-tree? pict?)])) |
#lang racket/base | |
;; This is a "static continuation" system, which I developed as a foundation for building macros like | |
;; the following: | |
;; | |
;; - The guard, guard-match, and guard-define statement macros in rebellion/private/guarded-block | |
;; - A (parameterize! id expr) statement macro that sets a parameter for the rest of the block | |
;; - An (open! disposable-expr) expression macro that allocates a resource and closes it at the end of | |
;; the block |
#lang racket/base | |
(require racket/generic | |
(prefix-in racket/list. racket/list) | |
racket/match | |
racket/stream | |
racket/struct | |
racket/unsafe/ops | |
(prefix-in racket/vector. racket/vector) |
#lang racket/base | |
(provide sdfn-register-id scalar-sdfn-register-id vec3-sdfn-register-id (struct-out sdfn-register)) | |
(require syntax/parse) | |
(struct sdfn-register (size offset initial-values) #:transparent) |
The function design recipe is a series of steps to follow when creating a function. The primary purpose of these steps is to help you think through the problem you're trying to solve. A secondary purpose is to make you write down explanations of what you're trying to do so that other programmers, especially programmers you ask for help, can understand your code.
In this article, we will be applying the function design recipe to the following example problem:
The state of Tax Land has created a three-stage sales tax to cope with its budget deficit. Inexpensive items, those costing less than $1,000, are not taxed. Luxury items, with a price of more than $10,000, are taxed at the rate of eight percent (8.00%). Everything in between comes with a five percent (5.00%) markup.
Design a function for a cash register that, given the price of an item, computes the sales tax.
#lang typed/racket | |
(define-type Vec2 (Vector Flonum Flonum)) | |
(: mad (Flonum Flonum Flonum . -> . Flonum)) | |
(define (mad a b c) | |
(+ (* a b) c)) | |
(: vec2-mad (Vec2 Vec2 Vec2 . -> . Vec2)) | |
(define (vec2-mad a b c) |