unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: guile-user@gnu.org
Subject: Re: Python slices in Scheme
Date: Sun, 18 Jun 2023 22:20:05 +0200	[thread overview]
Message-ID: <CADEOadcji1jcsesYBDwgtiWWdj7=F1G3vC308oNxmdxZrquMSA@mail.gmail.com> (raw)
In-Reply-To: <87pm5sd2nc.fsf@web.de>

[-- Attachment #1: Type: text/plain, Size: 720 bytes --]

Hello Arne,

yes it needs SRFI 105 Curly infix to allow full notation.
It defines the optional $bracket-apply$ procedure or macro (here a macro)
as described in SRFI 105. The code is in attachment (not in my github
because there is a lot of work again to have the same powerful and easy
affectation between 2 arrays than in Python)

Best regards,

Damien


On Sun, Jun 18, 2023 at 8:52 PM Dr. Arne Babenhauserheide <arne_bab@web.de>
wrote:

> Hi Damien,
>
> > {#(1 2 3 4 5 6 7)[2 / 5]}
> > #(3 4 5)
>
> that looks pretty interesting. Is it compatible to curly infix / SRFI-105?
>
> Best wishes,
> Arne
> --
> Unpolitisch sein
> heißt politisch sein,
> ohne es zu merken.
> draketo.de
>

[-- Attachment #2: apply-square-brackets.scm --]
[-- Type: application/octet-stream, Size: 22380 bytes --]

;; for guile (version compatible with my growable vector class)

;; This file is part of Scheme+

;; Copyright 2021-2023 Damien MATTEI

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.



;; SRFI 105 : Curly-infix-expressions in conjunction with specialized $bracket-apply$
;; of Scheme+ allows a syntax like {Container[index]} with vectors
;; and arrays of any dimensions,size and shape and hash tables

;; (define T (make-vector 5))
;; (vector-set! T 3 7)
;; scheme@(guile-user)> {T[3]}
;; $3 = 7
;; {T[3] <- 7}
;; 7

;; scheme@(guile-user)> (define a (make-array 999 '(1 2) '(3 4)))
;; scheme@(guile-user)> (array-ref a 2 4)
;; $3 = 999

;; scheme@(guile-user)> {a[2 4]}
;; $9 = 999

;; scheme@(guile-user)> (define b (make-array 'ho 3))
;; scheme@(guile-user)> (array-ref b 1)
;; $13 = ho

;; scheme@(guile-user)> {b[2]}
;; $15 = ho

;; scheme@(guile-user)> '{b[2]}
;; ($bracket-apply$ b 2)

;; scheme@(guile-user)> {a[2 4] <- 7}
;; scheme@(guile-user)> {a[2 4]}
;; $19 = 7
;; scheme@(guile-user)> {a[1 3] <- 5}
;; scheme@(guile-user)> {a[1 3] <- a[2 4]}
;; scheme@(guile-user)> {a[1 3]}
;; $20 = 7

;; scheme@(guile-user)> '{a[1 3] <- a[2 4]}
;; (<- ($bracket-apply$ a 1 3) ($bracket-apply$ a 2 4))



;; (define-syntax $bracket-apply$
;;   (syntax-rules ()
    
;;     ((_ container index)
;;      ;(begin ;;(display "$bracket-apply$") (newline)
;;      (cond ({(vector? container) or (growable-vector? container)} (if (equal? (quote ..) (quote index)) ;; T[1..5] Pascal syntax ;-)  
;; 								      container ;; return the vector
;; 								      (vector-ref container index))) ;; return an element of the vector
;; 	   ((hash-table? container) (hash-table-ref container index))
;; 	   ((string? container) (string-ref container index))
;; 	   (else (array-ref container index))));)
    
;;     ((_ array index1 index2 ...)
;;      ;(begin ;;(display "$bracket-apply$") (newline)
;;      (if (vector? array)
;; 	 (function-array-n-dim-ref array (reverse (list index1 index2 ...))) ;;(array-n-dim-ref array index1 index2 ...)
;; 	 (array-ref array index1 index2 ...)))));) 



(define slice /) ;;'..)

(define-syntax $bracket-apply$
  
  (syntax-rules ()


    ;; 1 argument in []
    ;; T[index]
    ((_ container index)

     ;; {#(1 2 3 4 5)[-2]}
     ;; 4
     
     (cond ((or (vector? container) (growable-vector? container))

	    (if (equal? slice index) ;; T[..] . T[1 .. 5] Pascal syntax ;-)  
		container ;; return the vector
		(if (< index 0) ;; negative index as in Python
		    (vector-ref container (+ (vector-length container) index)) ;; negative indexing
		    (vector-ref container index)))) ;; return an element of the vector
	   
	   ((hash-table? container) (hash-table-ref container index))

	   ;; sometimes i'm impress by Scheme :o
	   ;;  {"toto"[2]}
           ;; #\t
	   ;; {"toto"[-1]}
	   ;; #\o
	   ((string? container) (if (equal? slice index) ;; T[..] . T[1 .. 5] Pascal syntax ;-)  
				    container ;; return the string
				    (if (< index 0) ;; negative index as in Python
					(string-ref container (+ (string-length container) index)) ;; negative indexing
					(string-ref container index)))) ;; return an element of the string
	   
	   (else ;; array of SRFI 25	     
	    (array-ref container index)))) ;; return the element of the array
           ;; note : i do not use negative indexes or slices for array because they could be not starting at zero
    

    ;; 2 arguments in []
    ;; ex: T[i1 ..] , T[.. i2], T[i1 i2] , T[.. ..]
    ((_ container index1-or-keyword index2-or-keyword) 

     (cond ((vector? container) ;; 2 dimension vector ? or T[i1 ..] , T[.. i2]


	    ;; {#(1 2 3 4 5)[2 /]}
	    ;; '#(3 4 5)
	    ;;
	    ;; {#(1 2 3 4 5)[/ 3]}
	    ;; '#(1 2 3)
	    
	    (cond ((and (equal? slice index1-or-keyword) ;; T[.. ..]
			(equal? slice index2-or-keyword))
		   container)
		  
		  ((equal? slice index1-or-keyword) ;; T[.. i2]
		   (if (< index2-or-keyword 0) ;; negative index
		       (vector-copy container 0 (+ (vector-length container) index2-or-keyword))
		       (vector-copy container 0 index2-or-keyword)))
		  
		  ((equal? slice index2-or-keyword) ;; T[i1 ..]
		   (if (< index1-or-keyword 0) ;; negative index
		       (vector-copy container (+ (vector-length container) index1-or-keyword))
		       (vector-copy container index1-or-keyword)))
		  
		  (else ;; T[i1 i2] vector of vectors
		   (function-array-n-dim-ref container (reverse (list index1-or-keyword index2-or-keyword))))))
	   ;;(array-n-dim-ref container index1-or-keyword index2-or-keyword)


	   ;; {"hello"[/ 2]}
	   ;; "he"
	   ;; {"hello"[3 /]}
	   ;; "lo"

	   ;; {"hello"[/ /]}
	   ;; "hello"
	   ;; {"hello"[/]}
           ;; "hello"
	   ((string? container) (cond ((and (equal? slice index1-or-keyword) ;; T[/ /]
					    (equal? slice index2-or-keyword))
				       container)
				       
				       ((equal? slice index1-or-keyword) ;; T[/ i2]
					(if (< index2-or-keyword 0) ;; negative index
					    (substring container 0 (+ (string-length container) index2-or-keyword))
					    (substring container 0 index2-or-keyword)))

				       ((equal? slice index2-or-keyword) ;; T[i1 /]
					(if (< index1-or-keyword 0) ;; negative index
					    (substring container (+ (string-length container) index1-or-keyword))
					    (substring container index1-or-keyword)))
				      
				       (else ;; syntax error
					(error "$bracket-apply$ : bad arguments in string case,expecting / i2 or i1 /, provided :" index1-or-keyword index2-or-keyword) )))
	   
	   
	   (else ;; T[i1 i2] ,  2 dimension array
	    (array-ref container index1-or-keyword index2-or-keyword)))) ;; return the element of the array
            ;; note : i do not use negative indexes or slices for array because they could be not starting at zero
    

    ;; 3 arguments in []
    ;; T[i1 / i2] , T[i1 i2 i3] , T[/ / s]
    ((_ container index1-or-keyword index2-or-keyword index3-or-step) 

     ;; {#(1 2 3 4 5 6 7)[2 / 5]}
     ;; '#(3 4 5)
     (cond ((vector? container) ;; 3 dimension vector T[i1 i2 i3]? or T[i1 / i3]

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

	    ;; {#(1 2 3 4 5 6 7 8)[/ / -2]}
	    ;; '#(8 6 4 2)
	    (cond ((and (equal? slice index1-or-keyword) ;; T[/ / step]
			(equal? slice index2-or-keyword))

		   (when (= 0 index3-or-step)
			 (error "$bracket-apply$ : slice step cannot be zero"))
		   
		   (let* ((size-input (vector-length container))
			  (size (quotient size-input (abs index3-or-step)))
			  (result '())
			  (i 0))
		     
		     (when (not (= (modulo size-input index3-or-step) 0))
			   (set! size (+ 1 size)))
		     (set! result (make-vector size))

		     (if (< index3-or-step 0) ;; with negative index we start at end of vector (like in Python)
			 (for ((define k (- size-input 1)) (>= k 0) (set! k (+ k index3-or-step)))
			      (vector-set! result
					   i
					   (vector-ref container k))
			      (set! i (+ 1 i)))
			 (for ((define k 0) (< k size-input) (set! k (+ k index3-or-step)))
			      (vector-set! result
					   i
					   (vector-ref container k))
			      (set! i (+ 1 i))))
		     
		     result))
		  

		  ((equal? slice index2-or-keyword) ;; T[i1 / i3]
		
		   (let ((i1 index1-or-keyword)
			 (i3 index3-or-step))
		     (when (< i1 0) ;; negative index
			   (set! i1 (+ (vector-length container) i1)))
		     (when (< i3 0) ;; negative index
			   (set! i3 (+ (vector-length container) i3)))
		     (vector-copy container i1 i3)))
		
		  ;; T[i1 i2 i3] vector of vectors of vectors
		  (else (function-array-n-dim-ref container (list index3-or-step index2-or-keyword index1-or-keyword))))) ;; was reverse list ...
	          ;;or use array-n-dim-ref macro


	   
	   ;; {"elephant"[2 / 5]}
	   ;; "eph"
	   ;;  {"abcdefghijkl"[/ / 2]}
	   ;; "acegik"
	   ;; {"abcdefghijkl"[/ / -3]}
	   ;; "lifc"
	   ;; {"123456789"[ /  / -1]}
	   ;; "987654321"
	   ((string? container) ;; T[i1 / i3] or error
	    
	    (cond ((and (equal? slice index1-or-keyword) ;; T[/ / step]
			(equal? slice index2-or-keyword))

		   (when (= 0 index3-or-step)
			 (error "$bracket-apply$ : slice step cannot be zero"))
		   
		   (let* ((size-input (string-length container))
			  (size (quotient size-input (abs index3-or-step)))
			  (result '())
			  (i 0))
		     
		     (when (not (= (modulo size-input index3-or-step) 0))
			   (set! size (+ 1 size)))
		     
		     (set! result (make-string size))

		     (if (< index3-or-step 0) ;; with negative index we start at end of vector (like in Python)
			 (for ((define k (- size-input 1)) (>= k 0) (set! k (+ k index3-or-step)))
			      (string-set! result
					   i
					   (string-ref container k))
			      (set! i (+ 1 i)))
			 (for ((define k 0) (< k size-input) (set! k (+ k index3-or-step)))
			      (string-set! result
					   i
					   (string-ref container k))
			      (set! i (+ 1 i))))
		     
		     result))
		  
		  ((equal? slice index2-or-keyword) ;; T[i1 / i3]
		
		   (let ((i1 index1-or-keyword)
			 (i3 index3-or-step))
		     (when (< i1 0) ;; negative index
			   (set! i1 (+ (vector-length container) i1)))
		     (when (< i3 0) ;; negative index
			   (set! i3 (+ (vector-length container) i3)))
		     (substring container i1 i3)))

		  (else (error "$bracket-apply$ : in string case, provided too much arguments:" index1-or-keyword index2-or-keyword index3-or-step))))


	   
	   (else ;; T[i1 i2 i3] ,  3 dimension array
	    (array-ref container index1-or-keyword index2-or-keyword index3-or-step)))) ;; return the element of the array
            ;; note : i do not use negative indexes or slices for array because they could be not starting at zero
    

    ;; 4 arguments in []
    ;; T[/ i2 / s] , T[i1 / / s] , T[i1 / i3 /] , T[i1 i2 i3 i4]
    ((_ container index1-or-keyword index2-or-keyword index3-or-keyword index4-or-keyword-or-step) 
 
     (cond ((vector? container)

	    ;; {#(1 2 3 4 5 6 7 8 9)[/ 7 / 2]}
	    ;; '#(1 3 5 7)
	    ;; {#(1 2 3 4 5 6 7 8 9)[/ 6 / -1]}
	    ;; '#(6 5 4 3 2 1)
	    ;; {#(1 2 3 4 5 6 7 8 9)[/ 6 / -2]}
	    ;; '#(6 4 2)
	    ;; {#(1 2 3 4 5 6 7 8 9)[/ -3 / -2]}
	    ;; '#(6 4 2)
	    (cond ((and (equal? slice index1-or-keyword)  ;; T[/ i2 / s]
			(equal? slice index3-or-keyword))
		   
		   (when (= 0 index4-or-keyword-or-step)
			 (error "$bracket-apply$ : slice step cannot be zero"))
		    
		   (let* ((size 0) ;; result size
			  (result '())
			  (i 0)
			  (i2 index2-or-keyword))

		     (when (< i2 0) ;; negative index
			   (set! i2 (+ (vector-length container) i2)))

		     (set! size (quotient i2 (abs index4-or-keyword-or-step)))
		      
		     ;;(displayln size)
		     (when (not (= (modulo i2 index4-or-keyword-or-step) 0))
			   (set! size (+ 1 size)))
		      
		     ;;(displayln size)
		     (set! result (make-vector size))
		     
		     (if (< index4-or-keyword-or-step 0) 
			  
			 (for ((define k (- i2 1)) (>= k 0) (set! k (+ k index4-or-keyword-or-step)))
			      (vector-set! result
					   i
					   (vector-ref container k))
			      (set! i (+ 1 i)))
			  
			 (for ((define k 0) (< k i2) (set! k (+ k index4-or-keyword-or-step)))
			      (vector-set! result
					   i
					   (vector-ref container k))
			      (set! i (+ 1 i))))
		     
		     result))



		  ;; {#(1 2 3 4 5 6 7 8 9)[3 / / 2]}
		  ;; '#(4 6 8)
		  ;; > {#(1 2 3 4 5 6 7 8 9)[3 / / -2]}
		  ;; '#(4 2)
		  ;; > {#(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)
		  ((and (equal? index2-or-keyword slice)  ;; T[i1 / / s]
			(equal? index3-or-keyword slice))
		   
		    (when (= 0 index4-or-keyword-or-step)
			  (error "$bracket-apply$ : slice step cannot be zero"))
		   
		    (let* ((size-container (vector-length container))
			   (i1 (if (< index1-or-keyword 0) ;; negative index
				   (+ size-container index1-or-keyword)
				   index1-or-keyword))
			   (size-input (if (> index4-or-keyword-or-step 0)
					   (- size-container i1)
					   (+ i1 1)))
			   (size (quotient size-input (abs index4-or-keyword-or-step))) ;; result size
			   (result '())
			   (i 0))
		      
		      (when (not (= (modulo size-input index4-or-keyword-or-step) 0))
			    (set! size (+ 1 size)))
		      (set! result (make-vector size))

		      (if (< index4-or-keyword-or-step 0) 
			 
			  (for ((define k i1) (>= k 0) (set! k (+ k index4-or-keyword-or-step)))
			       (vector-set! result
					    i
					    (vector-ref container k))
			       (set! i (+ 1 i)))
			 
			  (for ((define k i1) (< k size-container) (set! k (+ k index4-or-keyword-or-step)))
			       ;;(displayln k)
			       (vector-set! result
					    i
					    (vector-ref container k))
			       (set! i (+ 1 i))))
		     
		      result))


		  ((and (equal? index2-or-keyword slice) ;; T[i1 / i3 /]
			(equal? index4-or-keyword-or-step slice))

		   (let ((i1 index1-or-keyword)
			 (i3 index3-or-keyword))
		     (when (< i1 0) ;; negative index
			   (set! i1 (+ (vector-length container) i1)))
		     (when (< i3 0) ;; negative index
			   (set! i3 (+ (vector-length container) i3)))
		     (vector-copy container i1 i3)))

		   
		   ;; T[i1 i2 i3 i4] vector of vectors of vectors ...
		   (else
		    
		    (function-array-n-dim-ref container (list index4-or-keyword-or-step index3-or-keyword index2-or-keyword index1-or-keyword))))) ;; was reverse list ...
	          ;;or use array-n-dim-ref macro

      
	   
	   ;; {"123456789"[/ -3 / -2]}
	   ;; "642"
	   ((string? container) 

	    ;; {"abcdefghijklmno"[/ 7 / 2]}
	    ;; "aceg"
	    ;; > {"123456789"[/ -3 / -2]}
	    ;; "642"
	    (cond ((and (equal? slice index1-or-keyword) ;; T[/ i2 / s]
			(equal? slice index3-or-keyword))
		   
		    (when (= 0 index4-or-keyword-or-step)
			  (error "$bracket-apply$ : slice step cannot be zero"))
		    
		    (let* ((size 0)
			   (result '())
			   (i 0)
			   (i2 index2-or-keyword))

		      (when (< i2 0) ;; negative index
			    (set! i2 (+ (string-length container) i2)))

		      (set! size (quotient i2 (abs index4-or-keyword-or-step)))
		     
		      (when (not (= (modulo i2 index4-or-keyword-or-step) 0))
			    (set! size (+ 1 size)))
		      (set! result (make-string size))

		      (if (< index4-or-keyword-or-step 0)
			 
			 (for ((define k (- i2 1)) (>= k 0) (set! k (+ k index4-or-keyword-or-step)))
			      (string-set! result
					   i
					   (string-ref container k))
			      (set! i (+ 1 i)))
			 
			 (for ((define k 0) (< k i2) (set! k (+ k index4-or-keyword-or-step)))
			      (string-set! result
					   i
					   (string-ref container k))
			      (set! i (+ 1 i))))
		     
		     result))


		  ;; {"abcdefghijklmno"[3 / / 2]}
		  ;; "dfhjln"
		  ;; > {"123456789"[3 /  / 2]}
		  ;; "468"
		  ;; > {"123456789"[3 /  / -2]}
		  ;; "42"
		  ;; > {"123456789"[-3 /  / -2]}
		  ;; "7531"
		  ;; > {"123456789"[-3 /  / 2]}
		  ;; "79"
		  ((and (equal? index2-or-keyword slice)  ;; T[i1 / / s]
			(equal? index3-or-keyword slice))
		   
		    (when (= 0 index4-or-keyword-or-step)
			  (error "$bracket-apply$ : slice step cannot be zero"))
		   
		    (let* ((size-container (string-length container))
			   (i1 (if (< index1-or-keyword 0) ;; negative index
				   (+ size-container index1-or-keyword)
				   index1-or-keyword))
			   (size-input (if (> index4-or-keyword-or-step 0)
					   (- size-container i1)
					   (+ i1 1)))
			   (size (quotient size-input (abs index4-or-keyword-or-step)))
			   (result '())
			   (i 0))
		      
		      (when (not (= (modulo size-input index4-or-keyword-or-step) 0))
			    (set! size (+ 1 size)))
		      (set! result (make-string size))

		      (if (< index4-or-keyword-or-step 0) 
			 
			  (for ((define k i1) (>= k 0) (set! k (+ k index4-or-keyword-or-step)))
			       (string-set! result
					    i
					    (string-ref container k))
			       (set! i (+ 1 i)))
			 
			  (for ((define k i1) (< k size-container) (set! k (+ k index4-or-keyword-or-step)))
			       ;;(displayln k)
			       (string-set! result
					    i
					    (string-ref container k))
			       (set! i (+ 1 i))))
		     
		      result))
	  
		 
		   ((and (equal? slice index2-or-keyword) ;; T[i1 / i3 /] 
			 (equal? slice index4-or-keyword-or-step))

		    (let ((i1  index1-or-keyword)
			  (i3 index3-or-keyword))
		      (when (< i1 0) ;; negative index
			    (set! i1 (+ (string-length container) i1)))
		      (when (< i3 0) ;; negative index
			    (set! i3 (+ (string-length container) i3)))
		      (substring container i1 i3)))

		   
		   ;; T[i1 i2 i3 i4] vector of vectors of vectors ... but we are in string context !!!
		   (else
	    
		    (error "$bracket-apply$ : in string case, provided too much arguments:" index1-or-keyword index2-or-keyword index3-or-keyword index4-or-keyword-or-step))))

	   
	   (else ;; T[i1 i2 i3 i4] ,  4 dimension array
	    (array-ref container index1-or-keyword index2-or-keyword index3-or-keyword index4-or-keyword-or-step)))) ;; return the element of the array
            ;; note : i do not use negative indexes or slices for array because they could be not starting at zero


    

    ;; 5 arguments in []
    ;; T[i1 / i3 / s] , T[i1 i2 i3 i4 i5]
    ((_ container index1 index2-or-keyword index3 index4-or-keyword index5-or-step) 

     ;; {#(1 2 3 4 5 6 7 8 9)[2 / 5 / 1]}
     ;; '#(3 4 5)
     ;; {#(1 2 3 4 5 6 7 8 9)[5 / 2 / -1]}
     ;; '#(6 5 4)
     ;; {#(1 2 3 4 5 6 7 8 9)[2 / 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)[-0 / 5 / -1]}
     ;; '#()
     (cond ((vector? container)
	    
	    (if (and (equal? index2-or-keyword slice)  ;; T[i1 / i3 / s]
		     (equal? index4-or-keyword slice))

		(begin
		   
		  (when (= 0 index5-or-step)
			(error "$bracket-apply$ : slice step cannot be zero"))
		  
		  (let* ((size-container (vector-length container))
			 
			 (i1 (if (< index1 0) ;; negative index
				 (+ size-container index1)
				 index1))

			 (i3 (if (< index3 0) ;; negative index
				 (+ size-container index3)
				 index3))
			  
			 (size-input (if (> index5-or-step 0)
					 (- i3 i1)
					 (- i1 i3)))
			  
			 (size (quotient size-input (abs index5-or-step))) ;; result size
			 (result '())
			 (i 0))
		      
		    (when (not (= (modulo size-input index5-or-step) 0))
			  (set! size (+ 1 size)))

		    (if (<= size 0)
			
			(make-vector 0)

			(begin
			  (set! result (make-vector size))
			  
			  (if (< index5-or-step 0) 
			      
			      (for ((define k i1) (> k i3) (set! k (+ k index5-or-step))) ;; we do not include i1-th element
				   ;; i do not allow Python index over size of vector
				   ;; (when (>= k size-container)
				   ;; 	 (continue))
				   
				   (vector-set! result
						i
						(vector-ref container k))
				   (set! i (+ 1 i)))
			      
			      (for ((define k i1) (< k i3) (set! k (+ k index5-or-step)))
				   ;;(displayln k)
				   (vector-set! result
						i
						(vector-ref container k))
				   (set! i (+ 1 i))))
		     
			  result))))


		;; T[i1 i2 i3 i4 i5] vector of vectors of vectors ...
		(function-array-n-dim-ref container (list index5-or-step
							  index4-or-keyword
							  index3
							  index2-or-keyword
							  index1)))) ;; was reverse list ...
                     ;;or use array-n-dim-ref macro

      
	   
	   ;; {"0123456789"[5 / 2 / -1]}
	   ;; "543"
	   ;; {"0123456789"[5 /  / -1]}
	   ;; "543210"
	   ;; {"0123456789"[5 / 0 / -1]}
	   ;; "54321"
	   ((string? container) 

	    (if (and (equal? index2-or-keyword slice)  ;; T[i1 / i3 / s]
		     (equal? index4-or-keyword slice))

		(begin
		   
		  (when (= 0 index5-or-step)
			(error "$bracket-apply$ : slice step cannot be zero"))
		  
		  (let* ((size-container (string-length container))
			 
			 (i1 (if (< index1 0) ;; negative index
				 (+ size-container index1)
				 index1))

			 (i3 (if (< index3 0) ;; negative index
				 (+ size-container index3)
				 index3))
			  
			 (size-input (if (> index5-or-step 0)
					 (- i3 i1)
					 (- i1 i3)))
			  
			 (size (quotient size-input (abs index5-or-step))) ;; result size
			 (result '())
			 (i 0))
		      
		    (when (not (= (modulo size-input index5-or-step) 0))
			  (set! size (+ 1 size)))

		    (if (<= size 0)
			
			(make-string 0)

			(begin
			  (set! result (make-string size))
			  
			  (if (< index5-or-step 0) 
			      
			      (for ((define k i1) (> k i3) (set! k (+ k index5-or-step))) ;; we do not include i1-th element
				   ;; i do not allow Python index over size of string
				   ;; (when (>= k size-container)
				   ;; 	 (continue))
				   
				   (string-set! result
						i
						(string-ref container k))
				   (set! i (+ 1 i)))
			      
			      (for ((define k i1) (< k i3) (set! k (+ k index5-or-step)))
				   ;;(displayln k)
				   (string-set! result
						i
						(string-ref container k))
				   (set! i (+ 1 i))))
		     
			  result))))

	   
		;; T[i1 i2 i3 i4 i5] vector of vectors of vectors ... but we are in string context !!!
		  
		(error "$bracket-apply$ : in string case, provided too much arguments:" index1 index2-or-keyword index3 index4-or-keyword index5-or-step)))

	   
	   (else ;; T[i1 i2 i3 i4 i5] ,  5 dimension array

	    (array-ref container index1 index2-or-keyword index3 index4-or-keyword index5-or-step)))) ;; return the element of the array
            ;; note : i do not use negative indexes or slices for array because they could be not starting at zero


    
    ;; more than 5 arguments in []
    ;; T[i1 i2 i3 i4 i5 ...]
    ((_ array index1 index2 index3 index4 index5 ...)
					
     (if (vector? array)
	 (function-array-n-dim-ref array (reverse (list index1 index2 index3 index4 index5 ...))) ;;(array-n-dim-ref array index1 index2 ...)
	 (array-ref array index1 index2 index3 index4 index5 ...)))))   ;; array SRFI 25



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

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-18 16:58 Python slices in Scheme Damien Mattei
2023-06-18 18:51 ` Dr. Arne Babenhauserheide
2023-06-18 20:20   ` Damien Mattei [this message]
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='CADEOadcji1jcsesYBDwgtiWWdj7=F1G3vC308oNxmdxZrquMSA@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).