Python Slice Sim
#1
Probably could use more testing but I thought this might be fun:

Code:
' slice test.bas SmallBASIC 0.12.9 (B+=MGA) 2017-05-23
' Attempt of sim of Python's slice, not nearly as elegant
' need 2 forms one for strings and one for single dimension array = list

func sSlice(s, start, finish) 'string Slice
 if start <= len(s) then
   if start > 0 then
     sSlice = iff( finish >= len(s), mid(s, start), mid(s, start, finish - start + 1) )
   else
     sSlice = iff( finish >= len(s), mid(s, 1), mid(s, 1, finish) )
   fi
 else
   sSlice = ""
 fi
end

func aSlice(a, start, finish) 'array (one dimension list)
 local ta, i
 dim ta()
 if start < lbound(a) then start = lbound(a)
 if finish > ubound(a) then finish = ubound(a)
 for i = start to finish
   ta << a(i)
 next
 aSlice = ta
end

test = "123456789"
? "sSlice test = ";sSlice(test,-1, 1);sSlice(test, 2, 4);sSlice(test,5, 15)

test = ["first", "second", "third", "forth", "fith", "sixth"]
? "aSlice = ";aSlice(test, -3, 2);aSlice(test, 3, 4);aSlice(test, 5, 15)

pause


Attached Files Thumbnail(s)

B += x
Reply
#2
very cool.

if you were working on your own language you should definitely add this feature.
Reply
#3
Yes, I thought this more powerful than a JB WORD$(s$, wNumber [, delimiter]) Sim for a parser, but JB is base 1 like the my sSlice for a string. So it might be confusing that aSlice is 0 based? Oh well...
B += x
Reply
#4
python is 0 based, so youre doing a good job.

fig is 1 based, because its actually inspired by the following languages in the following order: (consider this a defense of 1 based string/array handling)

* qbasic
* logo
* python
* forth / bash or bash / forth
Reply
#5
oooo
Oxygen can be 0 or 1 or any number based ...yes!
and also indexbase can be changed when program runing..

Mark
what a heck SHIFT_LEFT do in this FOR/LOOP ?
for i = start to finish
ta << a(i)
next

OR here in SB << is not SHIFT_LEFT ..awkward ?
Reply
#6
Hi Aurel,

I was beginning to wonder if your BOT joined the singularity.

"

Quote:
oooo
Oxygen can be 0 or 1 or any number based ...yes!
and also indexbase can be changed when program runing..

Mark
what a heck SHIFT_LEFT do in this FOR/LOOP ?
for i = start to finish
ta << a(i)
next

OR here in SB << is not SHIFT_LEFT ..awkward ?
"
Aurel
what a heck is SHIFT_LEFT?

r u referring 2

Code:
<<

?

EDIT 2017-06-30: which in SB means APPEND to ta() array the item a(i) (originally I said REPLACE but that was incorrect, where would you replace?)
B += x
Reply
#7
yeah when he said shift left i wondered what << was too. and in fact python has that as shift bits left <<

sb is interesting. when youre posting to a multi-dialect forum, youre not required to comment what that line does-- it does help though. we can just ask.
Reply
#8
Oh I like questions, I think I like them better than Likes.

It tells me not just who is following but how well they are following.

Feedback! I love feedback, specially if thoughtful.

Have you guys seen what Chris has posted at SB?

Do you know what he is talking about? How his one little change gives SB OOP potential?

Do you know the game 2048?
B += x
Reply
#9
im about as much a fan of oop as aurel is.

you can do closures in fig though. not that i ever do, but i did once.

chris is amazing ; everything in python is an object, but i dont worry about that. unless oop becomes a requirement in sb or "the easy way" loses status too much, i think it will be fine.
Reply
#10
Quote:
im about as much a fan of oop as aurel is.

you can do closures in fig though. not that i ever do, but i did once.

chris is amazing ; everything in python is an object, but i dont worry about that. unless oop becomes a requirement in sb or "the easy way" loses status too much, i think it will be fine.

oops, should have said "pseudo" oop, with new SELF keyword (Chris adding to SB)

"closures" refers to this "SELF" or "RETURN" keyword (also adding to SB)

I don't get OOP yet either but supposed to be very powerful.
B += x
Reply
#11
damn this forum just eat my post
it
yes << is shift bits left
OOP is not something super .Natural

Code:
Function HiWord (byval hi as long) as long
hi>>16 : function=hi  'here is shift right
End Function

oop

Code:
'CLASSES AND METHODS
'


  class stat
    method sum() as double
    method average() as double
    /\
    da(100) as double
    dn as long
  end class

  '--------------  
  methods of stat
  '==============
    
    method sum() as double
      dim i
      for i = 1 to dn
        method += da(i)
      next
    end method
    
    method average() as double
      method = sum() / dn
    end method
    
  end methods
  
  '====
  'TEST
  '====
  
  dim s as stat
  
  s.da=>2,4,6,8,10,12,14,16,18
  s.dn=9
  print `Sum: ` str(s.sum()) `       Average: ` str(s.average)
Reply
#12
im quite happy (sometimes) with oop as an underlying technology. other times its just bloat and overhead for no good reason.

i dont enjoy using anything when it lags, but i tolerate pythons lack of speed-- which most of the time i dont notice, because i dont have much interest in faster things that are less flexible.

but bring oop to the surface and it just gets in my way. i almost never use global variables in my functions, nor do i require instances of those functions with their own variables. nor do i need big globs of variables that are not an array or hash table. im not saying that those are useless things-- very useful for some purposes, but they are not terribly useful to me; more like a distraction. when blatant oop can be easily avoided, it keeps things simpler-- but if you like it, thats fine. its just not my thing.
Reply
#13
I have correction to make.

I told Aurel that
Code:
a << ta(i)

meant "replace" but it means to append to the a array the item ta(i)

I am sorry if I mislead anyone.
B += x
Reply