From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Ivan Kupalov Newsgroups: gmane.lisp.guile.user Subject: Some reader syntax for data structures Date: Mon, 25 Mar 2019 21:55:11 +0100 (CET) Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_155206_1925818669.1553547312153" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="8360"; mail-complaints-to="usenet@blaine.gmane.org" To: Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Mar 25 21:56:03 2019 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8WdX-000248-6g for guile-user@m.gmane.org; Mon, 25 Mar 2019 21:56:03 +0100 Original-Received: from localhost ([127.0.0.1]:48397 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8WdW-0006el-5z for guile-user@m.gmane.org; Mon, 25 Mar 2019 16:56:02 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:36435) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8Wcv-0006br-EM for guile-user@gnu.org; Mon, 25 Mar 2019 16:55:26 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8Wct-00047V-Nz for guile-user@gnu.org; Mon, 25 Mar 2019 16:55:25 -0400 Original-Received: from w4.tutanota.de ([81.3.6.165]:38800) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h8Wco-0003rE-Oi for guile-user@gnu.org; Mon, 25 Mar 2019 16:55:20 -0400 Original-Received: from w2.tutanota.de (unknown [192.168.1.163]) by w4.tutanota.de (Postfix) with ESMTP id 26A8E10600E9 for ; Mon, 25 Mar 2019 20:55:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tutanota.com; s=20161216; t=1553547312; bh=0IC81mWSYx9YxhyTzWt1DAaM/iSzrB/WRn7m8aurWL8=; h=Date:From:To:Subject:From; b=uTp7QPhgmQV12z3RX7ImdkrdXqouA4eMyemN82gT9bhgjEIRI+lde+Km534AVUfHU Kl567PiaV7vqA2n7oAjdLifqwJ8TPYIU1c+fc2M8R9FqIEqdoQK0efr0GHEPvM3cQ7 YhupeE+q6XE3dkNshUot408pidbP7e55EbOAa0jIknDDo9MUzl4vLAQqiP5kCqIHMm QzDu0Vr9t9MhaBclansG/Reb/7vFgr+oWrsy6awBk2WuajFXpcUZqS61kd4YIpoOum Kw3rH6RTLDcLsR3EGjFKjC+aCWOuMpG3Ethnq8fRSnY3OyV/h08Ze8uhG+IBDkQQB3 DoSoC3VRELRHA== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 81.3.6.165 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:15371 Archived-At: ------=_Part_155206_1925818669.1553547312153 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hello everyone! I wanted to learn some Scheme before starting one project and it seemed to me that it might be a nice exercise to try and extend syntax for hash tables, a-la Clojure/Rackjure (and only later I've learned about Racket's `#hash()`). I think it's really not optimized and not even really elegant but it works! (readers don't have much documentation attached to them unlike macros so it was kinda difficult). I later extended it to vhashes and vlists because I already had some understanding on what is required from me. (also here's a link in case something doesn't work, I didn't use mailing lists much before: https://gitlab.com/snippets/1838863) So I just wanted to share. Feel free to send feedback! ------=_Part_155206_1925818669.1553547312153 Content-Type: text/x-scheme; charset=us-ascii; name=collection-readers.scm Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=collection-readers.scm (use-modules (ice-9 match)) (use-modules (ice-9 vlist)) (use-modules (srfi srfi-1)) (eval-when (expand load eval) (define (read-hash-table-2 datum) (let ((insertions (map (match-lambda ((key value) `(hash-set! table ,key ,value))) datum))) (append `(let ((table (make-hash-table ,(length insertions))))) insertions '(table)))) (define (read-vhash-table datum) (let ((datum (reverse datum))) (fold (lambda (el acc) (match el ((key value) `(vhash-consq ,key ,value ,acc)) )) 'vlist-null datum))) (define (read-vlist datum) (list 'list->vlist (cons 'list datum))) (read-hash-extend #\h (lambda (chr port) (read-hash-table-2 (read port)))) (read-hash-extend #\v (lambda (chr port) (let ((type (read port)) (definition (read port))) (cond ((eq? 'h type) (read-vhash-table definition)) ((eq? 'l type) (read-vlist definition)) (else (error "woah I don't know this syntax"))))))) (define characters #h(('you "hooman") ('me "schemer"))) (display "I am a ") (display (hash-ref characters 'me)) ;; => schemer (newline) (define demo-table #h( ('name "Demo name") ('version "3.45.5") ('dependencies #h( ('hash-map-syntax "0.0.1"))) ((string-append "computed-" "key") #t))) (display (hash-ref demo-table "computed-key")) ;; => #t (newline) (define demo-vhash #vh( ('vh "vhash") ('vl "vlist"))) (define demo-vlist #vl( "Nice" "Syntax" #vl( "For" (string-append "Efficient" " " "structures")))) ------=_Part_155206_1925818669.1553547312153--