Skip to content

Instantly share code, notes, and snippets.

@vbuaraujo
Last active June 5, 2016 17:05
Show Gist options
  • Save vbuaraujo/b6e19a3b887e94080e53c97a0ae91022 to your computer and use it in GitHub Desktop.
Save vbuaraujo/b6e19a3b887e94080e53c97a0ae91022 to your computer and use it in GitHub Desktop.
stitchcounter (from @manuel_uberti)
;;; Library io-utils.scm
;; Common I/O utilities
(declare (unit io-utils))
(module io-utils *
(import scheme chicken data-structures)
(use extras)
(use input-parse)
;; Print a prompt and wait for an input from the user
(define (get-input prompt)
(printf "~A" prompt)
(read-text-line))
;; Print a prompt and specifically ask for a number
(define (get-num prompt)
(let* ((v (get-input prompt))
(vl (string-split v " " #f))
(vc (if (pair? vl) (car vl) "")))
(if (number? (string->number vc))
(string->number vc)
(get-num (conc "Please specify a valid number.\n" prompt)))))
)
# -J generates the module's import file, which is necessary for CHICKEN
# to know which bindings are available in the module. I compiled each file
# separately, but you could have just run 'csc -J *.scm' instead (as long
# as the files were provided in order, I guess, since to compile
# stitchcounter.scm you need the imports file from io-utils).
just_do_it:
csc -c -J io-utils.scm
csc -c -J stitchcounter.scm
csc io-utils.o stitchcounter.o
#!/bin/sh
#| -*- scheme -*-
exec csi -s $0 "$@"
|#
;;; stitchcounter.scm
(declare (uses io-utils))
(module stitchcounter *
(import scheme chicken io-utils)
(define cms-per-square 10.0)
;; Calculate stitches from the original gauge
(define (option-1)
(let* ((orows
(get-num "Number of rows/rounds as listed in pattern gauge: "))
(osts
(get-num "Number of stitches as listed in pattern gauge: "))
(rows
(get-num "Number of rows/rounds in gauge you want to use: "))
(sts
(get-num "Number of stitches in gauge you want to use: "))
(ovalr
(get-num "Total rows/rounds of pattern: "))
(resr
(round (/ (* rows ovalr) orows)))
(ovals
(get-num "Stitches to CO for your project: "))
(ress
(round (/ (* sts ovals) osts))))
(printf "Final rows/rounds: ~A / Final stitches: ~A\n" resr ress)))
;; Calculate stitches from dimensions
(define (option-2)
(let* ((units
(get-num "Dimension of swatch [4 inches, 10 cms, etc.]: "))
(rows
(get-num "Rows/rounds in gauge: "))
(sts
(get-num "Stitches in gauge: "))
(rows1cm
(/ rows units))
(sts1cm
(/ sts units))
(cm
(get-num "Cms/inches of your project: "))
(resr
(* rows1cm cm))
(ress
(* sts1cm cm)))
(printf "Final rows/rounds: ~A / Final stitches: ~A\n" resr ress)))
(define (pick prompt)
(let ((input (get-num prompt)))
(cond
((= 1 input) (option-1))
((= 2 input) (option-2))
((= 3 input) (exit))
(else (pick prompt)))))
;; Main
(define (stitchcounter)
(print "\nStitchcounter: useful tool to help knitting and crocheting")
(print "Options:\n")
(print "\t1 Change gauge in a pattern")
(print "\t (Pattern lists gauge and width, you want to knit")
(print "\t the same width with a different gauge)\n")
(print "\t2 Calculate number of stitches to CO")
(print "\t (You have a given gauge, calculate how many stiches/rows")
(print "\t you need to get a given dimension)\n")
(print "\t3 Quit")
(pick "\nPick your option: "))
(stitchcounter)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment