| Bytes | Lang | Time | Link |
|---|---|---|---|
| 071 | tinylisp | 250206T080100Z | Mukundan |
| 8875 | Vyxal | 230603T115903Z | lyxal |
| 020 | Japt h | 210326T103704Z | AZTECCO |
| 017 | Vyxal | 210608T042201Z | emanresu |
| 086 | Racket | 210325T182905Z | hyperneu |
| 104 | Common Lisp | 210328T032153Z | ASCII-on |
| 100 | Lua | 210326T174438Z | val - di |
| 009 | Jelly | 210325T150326Z | caird co |
| 057 | Factor | 210326T000504Z | chunes |
| 054 | Python 3 | 210326T061513Z | kops |
| 075 | Python 3 | 210325T152937Z | Delfad0r |
| 048 | JavaScript V8 | 210325T152655Z | user8165 |
| 035 | J | 210325T231111Z | Jonah |
| 073 | Retina 0.8.2 | 210325T233257Z | Neil |
| 020 | Charcoal | 210325T231100Z | Neil |
| 069 | Jq | 210325T194909Z | Wezl |
| 017 | Pyth | 210325T171342Z | hakr14 |
| 010 | Stax | 210325T155519Z | Razetime |
| 060 | Scala | 210325T151402Z | user |
| 072 | JavaScript V8 | 210325T145519Z | rydwolf |
| 086 | Python 3 | 210325T145358Z | hyperneu |
tinylisp, 71 bytes
(d F(q((A L)(i(t A)((i(e(h A)97)h t)(F(t A)L))L
(q((L S)(F(t(chars S))L
(q((L S)(F(t(chars S))L # anonmyous lambda
(L S) # args L (list), S (accessor as string)
( # return
F # F(
(t(chars S)) # S[1:] converted to ascii values,
L # L
# )
(d F(q((A L)(i(t A)((i(e(h A)97)h t)(F(t A)L))L
(d F # F = lambda
(A L) # args A (accessor as ascii values), L (list)
i(t A) # if A has more than 1 element:
(F(t A)L) # F(A[1:], L)
i(e(h A)97) # if A[0] == 97:
h # take first element of recursive call
t # else: remove first element of recursive call
L # else: return L as is
Vyxal, 71 bitsv2, 8.875 bytes
ḢṪṘ(₍ḢhnAi
Explained
ḢṪṘ(₍ḢhnAi
ḢṪ # Remove the c and r from the input
Ṙ # and reverse it
( # for each char
₍Ḣh # [top_of_stack[1:], top_of_stack[0]]
i # indexed by
nA # is char a vowel?
Japt -h, 20 bytes
VÔkric)¬£=X¥'a?UÎ:UÅ
- saved 2 thanks to @Shaggy
1st input U = data
2nd input V = command
VÔk"cr" - reverse and remove 'cr'
£ ... Ã - pass each remaining letters to :
= - reassign 1st input(U)
X¥'a?UÎ - 1st item if 'a'
:UÅÃ - else the rest
-h flag to output result
Racket, 86 bytes
(λ(a c)(foldr(λ(c a)(case c[(#\a)(car a)][(#\d)(cdr a)][else a]))a(string->list c)))
-3 bytes thanks to Wezl
-73 bytes thanks to ASCII-only
-8 bytes thanks to MLavrentyev
Input in the format (f <data> <string>)
Of course there has to be a lisp/scheme/racket/whatever answer :P
This is probably pretty bad because it's my first time golfing in Racket. I just know what I was taught in my basic first-year CS courses :P
Common Lisp, 106 104 bytes
(lambda(a c)(reduce(lambda(a c)(if(eq c #\a)(car a)(if(eq c #\d)(cdr a)a)))(reverse c):initial-value a))
Direct port of the racket answer.
Lua, 100 bytes
load[[t,c=...c:reverse():gsub('[ad]',load"if...=='a'then t=t[1]else table.remove(t,1)end")return t]]
Function that accepts a nested table structure and a string. I sure would be impressed to see this golfed further. Please note that original table structure will be modified when using d command, so don't pass it there if it have value to you (or, better yet, never use this code outside of TIO sandbox).
TIO link also includes a test suite.
Jelly, 11 9 bytes
Oị⁾ḢḊḊṖUv
-2 bytes thanks to Unrelated String!
Takes the accessor on the left and the array on the right
Oị⁾ḢḊḊṖUv - Main link. Accessor A on left, Array L on right
O - Convert A to ordinals. Note that 'a' is odd and 'd' is even
ị⁾ḢḊ - Index into "ḢḊ". Odd characters yield "Ḣ" and even "Ḋ"
ḊṖ - Remove first and last values from A
U - Reverse
v - Evaluate as Jelly code, with L as the argument
Ḣ is the head command, which returns the first element of a list. Ḋ is the dequeue command, which removes the first element of a list. The code executed by v is just a series of Ḣ and Ḋ atoms, which perform one after another.
Factor, 61 57 bytes
[ rest reverse rest [ 97 = [ first ] [ rest ] if ] each ]
-4 bytes thanks to @Neil!
Explanation:
This is a quotation (anonymous function) that takes two sequences from the data stack and leaves one sequence on the data stack. In Factor, strings are just sequences of unicode code points and can be manipulated like any other sequence.
- Given input
{ 0 { 1 2 } 3 } "cadr"... resttake everything but the first element of a sequence{ 0 { 1 2 } 3 } "adr"reversereverse a sequence{ 0 { 1 2 } 3 } "rda"resttake everything but the first element of a sequence{ 0 { 1 2 } 3 } "da"[ ... ] eachfor each element in the sequence...- At the beginning of the first iteration, the data stack looks like this:
{ 0 { 1 2 } 3 } 100 97push97on the data stack{ 0 { 1 2 } 3 } 100 97=test top two objects on the data stack for equality{ 0 { 1 2 } 3 } f[ first ] [ rest ] ifrun first quotation if TOS ist, otherwise run the second{ { 1 2 } 3 }- Now the next iteration of
eachwill kick in and put the next command on the data stack{ { 1 2 } 3 } 97 - Etc.
Python 3, 68 66 54 bytes
lambda r,s:eval(s[:0:-1].translate(["[1:]","[0]"]*51))
Alt, similar idea (which works in Python 2):
Python 3, 68 66 bytes
lambda l,c:eval(l+''.join("[[10:]]"[x<'d'::2]for x in c[-2:0:-1]))
Both create an eval string which just appends the desired sequence of subscripts/slices directly to the list, with the first one using the added trick of keeping the "r" as the list name. The first one takes an actual list as input and the second takes a string representation of a list.
Credit to xnor for pointing me at translate() and the idea of using r as the list name for -12 bytes!
Python 3, 80 79 75 bytes
f=lambda s,x:eval(['%s[1:]','%s[0]','x#%s','%s'][ord(s[0])%4]%'f(s[1:],x)')
By sheer luck, ord(s[0])%4 is able to distinguish between a, c, d, r.
JavaScript (V8), 48 bytes
a=>f=b=>b[2]?b[[x,...r]=f(b.slice(1)),1]<f?x:r:a
-2 bytes thanks to @Arnauld!
J, 35 bytes
4 :0
".'y',~(}.}:x)rplc;:'a{.d}.'
)
Note: -3 off TIO count for f=:
- Drop the first and last element from the
c[ad]+rdirective with}.}:x, leaving us with a string ofas andds. - Replace every
awith{.and everydwith}., J's version of car and cdr:rplc;:'a{.d}.' - Append the nested array we want to evaluate
'y',~. - And evaluate it
"..
Retina 0.8.2, 73 bytes
+`(a|(d))r.(((\[)|(?<-5>])|\d|(?(5),))+),?(?(2)(?<3>.*)|.*)
r$#2$*[$3
cr
Try it online! Link includes test cases. Explanation:
+`
Repeat until a match cannot be made.
(a|(d))r.(((\[)|(?<-5>])|\d|(?(5),))+),?(?(2)(?<3>.*)|.*)
Match either an a or d (remembering which), then an implied [, then a sequence of [s, ]s, ,s and digits, except no more ]s than [s and only matching ,s between [s and ]s, then an optional ,, then if it was a d that matched then replace the match of the first term with the tail of the list otherwise just delete the tail.
r$#2$*[$3
Keep the r, re-insert the [ if the d was matched, and insert the first term or tail as matched above.
cr
Finally delete the remaining cr.
Charcoal, 20 bytes
F⮌η≡ιa≔§θ⁰θd≔ΦθλθcIθ
Try it online! Link is to verbose version of code. Outputs using Charcoal's default format, which is each array element on its own line and each subarray double-spaced from the next (+1 byte to output in Python str format). Explanation:
F⮌η≡ι
Loop over the command characters in reverse order and switch on them.
a≔§θ⁰θ
For a replace the input with its first element.
d≔Φθλθ
For d filter the first element from the input.
cIθ
For c print the result.
Jq, 69 bytes
Takes an object with keys "c" for the accessor and "l" for the list.
reduce((.c/"")[1:-1]|reverse[])as$c(.l;if$c=="d"then.[1:]else.[0]end)
Explanation
reduce $0 as$c($1; $2 )
# the accumulator starts at $1. For each $0 (stored as the variable $c),
# it becomes the result of $2 applied to it.
((.c/"")[1:-1]|reverse[])
# the command (.c), split (/""), with the ends snapped ([1:-1])
# reversed (reverse), every value inside this array ([])
.l;if$c=="d"then.[1:]else.[0]end
# starting with the list (.l), if the character is "d" then the list
# becomes its tail (.[1:]), otherwise its head (.[0]).
Pyth, 17 bytes
.v+PtXQ"da""th"\E
Uses pretty much the same approach as ChartZ Belatedly's Jelly answer.
As an aside, this answer could shed 3/4 bytes if Pyth had an 'evaluate with argument' builtin, and an additional 1/2 if it had Jelly's 'transliterate' (Pyth builtins can be one or two bytes). This would put it at 11/12/13 bytes, which at best ties the 11 bytes Jelly takes. This is especially impressive since Jelly has to reverse the accessor, while Pyth doesn't (Pyth uses reverse Polish notation).
Stax, 10 bytes
Ç≈≡ø*fáì☺○
Returns a string of codepoints if array, and number if it's a number. The code can be verified here: Try it
Explanation
"ahdD"|trDl
"ahdD"|t translate 'a' to 'h' and 'd' to 'D'
r reverse
D delete first element
l eval
checking code:
cc|4{|u}a?
cc dup twice
|4 ? if it's an array:
{|u} convert to JSON
a otherwise leave as is
Scala, 60 bytes
_.tail.init.:\(_){case(x,m:Seq[_])=>if(x<98)m(0)else m.tail}
I hate nested lists. Because of them, we need a pattern matching function (or casts, which are longer).
_ //The command ("caadddaddaar")
.tail //Drop the 'c'
.init //Drop the 'r'
.:\ //Fold right the "aadddada"
(_) //Starting with the inputted list
{ //Pattern matching function literal
case (x, m: Seq[_]) => //The command is x, the list is m (must be a Seq)
if (x < 98) m(0) //If the command is 'a', return the first element
else m.tail //Else, drop the first element
}
JavaScript (V8), 77 72 bytes
c=>d=>[...c.slice(1)].reverse(d=[d]).map(n=>d=n=="d"?d.slice(1):d[0])&&d
Turns out the boring solution wins
JavaScript (V8), 91 90 87 84 bytes
c=>d=>[...c.slice(1)].map(n=>x=>n=="d"?x.slice(1):x[0]).reduceRight((a,f)=>f(a),[d])
Can't add a TIO link on school WiFi :/
Explanation:
This is a cool approach, using functional stuff. It slices the command name to remove the c, splits it into characters, then maps them to functions:
dbecomesx=>x.slice(1)aandrbecomex=>x[0]
My current approach actually returns one function, which can still access the n variable, which saves three bytes. I don't really know how to explain it but it works :p
It then uses reduceRight to apply those functions in reverse order, on [d]. This saves a byte over my old approach, which also sliced off the r.
Python 3, 86 bytes
lambda l,c,a=lambda x:x[0],d=lambda x:x[1:]:eval("(".join(d(c))[:-1]+l+")"*(len(c)-2))
-1 byte thanks to @Stephen