unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: guile-user <guile-user@gnu.org>
Subject: Python slices in Scheme
Date: Sun, 18 Jun 2023 18:58:54 +0200	[thread overview]
Message-ID: <CADEOadfkHYKRNhR0hqfiFcbRaxJ4-2Hg7dU=Js4PsJL1ryx50g@mail.gmail.com> (raw)

hello,

i'm porting the Python slicing (
https://docs.python.org/2/reference/expressions.html#slicings ) to Scheme
Guile and Racket.

examples in Scheme+ :

{#(1 2 3 4 5 6 7)[2 / 5]}
#(3 4 5)

i'm using / instead of : because : is already used by the SRFI 42 Eager
Comprehension

below are my testing examples:

;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/usr/local/share/guile/site/3.0/Scheme+.scm.go

scheme@(guile-user)> {#(1 2 3 4 5 6 7 8)[/ / 3]}
$1 = #(1 4 7)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8)[/ / -2]}
$2 = #(8 6 4 2)
scheme@(guile-user)> {"elephant"[2 / 5]}
$3 = "eph"
scheme@(guile-user)> {"abcdefghijkl"[/ / -3]}
$4 = "lifc"
scheme@(guile-user)> {"123456789"[ /  / -1]}
$5 = "987654321"
scheme@(guile-user)> {"abcdefghijkl"[/ / 2]}
$6 = "acegik"
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ 7 / 2]}
$7 = #(1 3 5 7)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ 6 / -1]}
$8 = #(6 5 4 3 2 1)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ 6 / -2]}
$9 = #(6 4 2)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[/ -3 / -2]}
$10 = #(6 4 2)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[3 / / 2]}
$11 = #(4 6 8)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[3 / / -2]}
$12 = #(4 2)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-3 / / 2]}
$13 = #(7 9)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-3 / / -2]}
$14 = #(7 5 3 1)
scheme@(guile-user)> {"123456789"[/ -3 / -2]}
$15 = "642"
scheme@(guile-user)> {"abcdefghijklmno"[/ 7 / 2]}
$16 = "aceg"
scheme@(guile-user)> {"123456789"[/ -3 / -2]}
$17 = "642"
scheme@(guile-user)> {"abcdefghijklmno"[3 / / 2]}
$18 = "dfhjln"
scheme@(guile-user)> {"123456789"[3 /  / 2]}
$19 = "468"
scheme@(guile-user)> {"123456789"[3 /  / -2]}
$20 = "42"
scheme@(guile-user)> {"123456789"[-3 /  / -2]}
$21 = "7531"
scheme@(guile-user)> {"123456789"[-3 /  / 2]}
$22 = "79"
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[2 / 5 / 1]}
$23 = #(3 4 5)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[5 / 2 / -1]}
$24 = #(6 5 4)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[2 / 5 / -1]}
$25 = #()
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-1 / 5 / -1]}
$26 = #(9 8 7)
scheme@(guile-user)> {#(1 2 3 4 5 6 7 8 9)[-0 / 5 / -1]}
$27 = #()
scheme@(guile-user)> {"0123456789"[5 / 2 / -1]}
$28 = "543"
scheme@(guile-user)> {"0123456789"[5 /  / -1]}
$29 = "543210"
scheme@(guile-user)> {"0123456789"[5 / 0 / -1]}
$30 = "54321"

the syntax is derived from Python syntax:

[1,2,3,4,5,6,7,8,9][-3::2]
[7, 9]
[1,2,3,4,5,6,7,8,9][-3::-2]
[7, 5, 3, 1]
[1,2,3,4,5,6,7,8,9][3::-2]
[4, 2]
"abcdefghijkl"[: : -3]
'lifc'
[1,2,3,4,5,6,7,8,9][:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][:5:1]
[1, 2, 3, 4, 5]
[1,2,3,4,5,6,7,8,9][0:5:-1]
[]
[1,2,3,4,5,6,7,8,9][-0:5:-1]
[]
[1,2,3,4,5,6,7,8,9][-1:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][10:5:-1]
[9, 8, 7]
[1,2,3,4,5,6,7,8,9][2:5:-1]
[]
[1,2,3,4,5,6,7,8,9][5:2:-1]
[6, 5, 4]
[0,1,2,3,4,5,6,7,8,9][5:2:-1]
[5, 4, 3]
[0,1,2,3,4,5,6,7,8,9][5::-1]
[5, 4, 3, 2, 1, 0]
[0,1,2,3,4,5,6,7,8,9][5:0:-1]
[5, 4, 3, 2, 1]

for now it works in simple expressions,soon it will be implemented for
expressions with LHS and RHS like {container1[2 / 5] <- container2[7 / 9]}
for this reason all will be in the next version of Scheme+
https://github.com/damien-mattei/Scheme-PLUS-for-Guile
with other features too.

Damien


             reply	other threads:[~2023-06-18 16:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-18 16:58 Damien Mattei [this message]
2023-06-18 18:51 ` Python slices in Scheme Dr. Arne Babenhauserheide
2023-06-18 20:20   ` Damien Mattei
2023-06-18 21:44     ` Dr. Arne Babenhauserheide
2023-06-19  1:52 ` Jay Sulzberger
2023-06-19 16:36   ` lloda
2023-06-19 19:44     ` Zelphir Kaltstahl
2023-06-20  6:39     ` Damien Mattei
2023-06-23 13:26     ` Jay Sulzberger
2023-06-23 19:14     ` Damien Mattei
2023-06-20  6:41   ` Damien Mattei
2023-06-20 11:22     ` Robby Zambito
2023-06-21  7:45       ` Damien Mattei
2023-06-23 13:17 ` Jay Sulzberger
2023-06-24 13:08   ` Damien Mattei
2023-06-24 16:18     ` Dr. Arne Babenhauserheide
2023-06-24 20:24       ` Damien Mattei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADEOadfkHYKRNhR0hqfiFcbRaxJ4-2Hg7dU=Js4PsJL1ryx50g@mail.gmail.com' \
    --to=damien.mattei@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).