view src/ploki/examples/perl-util.pk @ 12518:2d8fe55c6e65 draft default tip

<int-e> learn The password of the month is release incident pilot.
author HackEso <hackeso@esolangs.org>
date Sun, 03 Nov 2024 00:31:02 +0000
parents ac0403686959
children
line wrap: on
line source

GOTO 666

FOR map LEET f \@ . 0
	LEET list \@ . 1
	LEET result #<#>
	LEET i 0
	10 IF i < @LENGTH list
		LET result _= f . (list . i)
		LET i += 1
		GOFOR 10
	FI
KTHX result

FOR grep LEET f \@ . 0
	LEET list \@ . 1
	LEET result #<#>
	LEET i 0
	10 IF i < @LENGTH list
		IF f . (list . i)
			LET result _= #<(list . i)#>
		FI
		LET i += 1
		GOFOR 10
	FI
KTHX result

FOR s/// LEET str \@ . 0
	LEET pat \@ . 1
	LEET repl \@ . 2
	LEET ms str ~ pat
	IF ms
		LEET me \_
		IF @TYPE OF repl : "stuff"
			LET repl .= str [ ms ] (me - ms)
		FI
		LET str str ] ms _ repl _ (str [ me)
	FI
KTHX str

FOR s///g LEET str \@ . 0
	LEET pat \@ . 1
	LEET repl \@ . 2
	LEET new
	LEET p 0
	LEET ms
	LEET me
	10 LET ms str [ p ~ pat
	IF ms
		LET me \_
		LET new repl
		IF @TYPE OF new : "stuff"
			LET new .= str [ p [ ms ] (me - ms)
		FI
		LET new @STR new
		LET str str ] (p + ms) _ new _ (str [ (p + me))
		LET p += ms = me + ms + @LENGTH new
		IF @NOT (p > @LENGTH str)
			GOFOR 10
		FI
	FI
KTHX str

200 LEET b \@.0 _ @REVERSE (\@.1)
	LEET result #<#>
	LEET d
	LEET i 0
	LEET j @LENGTH b - 1
	210 IF i < j
		LET d cmp.#<(b.i) (b.j)#>
		IF d < 0
			LET result _= b [ i ] 1
			LET i += 1
		ELSE
			IF d > 0
				LET result _= b [ j ] 1
				LET j -= 1
			ELSE
				LET result _= b [ i ] 1
				LET i += 1
				LET result _= b [ j ] 1
				LET j -= 1
			FI
		FI
		GOFOR 210
	ELSE
		IF i = j
			LET result _= b [ i ] 1
		FI
	FI
KTHX result

FOR sort LEET cmp \@.0
	LEET list \@.1
	IF @LENGTH list < 2
		KTHX list
	FI
	LEET mid @INT (@LENGTH list / 2)
	ABRUF 200 #<@sort #<cmp (list ] mid)#> @sort #<cmp (list [ mid)#>#>
KTHX \_

FOR join LEET sep \@.0
	LEET list \@.1
	LEET result ""
	IF @LENGTH list
		LET result _= list.0
		LEET i 1
		10 IF i < @LENGTH list
			LET result _= sep
			LET result _= list.i
			LET i += 1
			GOFOR 10
		FI
	FI
KTHX result

FOR split LEET pat \@.0
	LEET str \@.1
	LEET result #<#>
	IF "" ~ pat
		LEET i 0
		10 IF i < @LENGTH str
			LET result _= #<(str . i)#>
			LET i += 1
			GOFOR 10
		FI
	ELSE
		LEET p 0
		LEET ms
		20 LET ms str [ p ~ pat
		IF ms
			LET result _= #<(str [ p ] ms)#>
			LET p += \_
			GOFOR 20
		FI
		LET result _= #<(str [ p)#>
	FI
KTHX result

FOR dumper LEET x \@
	IF @TYPE OF x : "number"
		KTHX x
	FI
	IF @TYPE OF x : "stream"
		KTHX "{IO}" _ x
	FI
	IF @TYPE OF x : "stuff"
		KTHX "{EXPR}?"
	FI
	IF @TYPE OF x : "nothing"
		KTHX "()"
	FI
	IF @TYPE OF x : "list"
		KTHX "#<" _ @join #<" " @map #<@OMFG #<@dumper \@#> x#>#> _ "#>"
	FI
KTHX "\"" _ @s///g #<x "'P!\\\"'" @OMFG "\\\V@REVERSE (@ORD \@ ` 8)"#> _"\""

FOR x LEET s \@ . 0
	LEET n \@ . 1
	LEET res ""
	10 IF n > 0
		LET res _= s
		LET n -= 1
		GOFOR 10
	FI
KTHX res

666