Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,17 @@
set stringA to "I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasnt really happy."
set string1 to "I felt happy"
set string2 to "I should feel happy"
set string3 to "I wasn't really happy"
-- Determining if the first string starts with second string
stringA starts with string1 --> true
-- Determining if the first string contains the second string at any location
stringA contains string2 --> true
-- Determining if the first string ends with the second string
stringA ends with string3 --> false
-- Print the location of the match for part 2
offset of string2 in stringA --> 69

View file

@ -0,0 +1,23 @@
-- Handle multiple occurrences of a string for part 2
on offset of needle in haystack
local needle, haystack
if the needle is not in the haystack then return {}
set my text item delimiters to the needle
script
property N : needle's length
property t : {1 - N} & haystack's text items
end script
tell the result
repeat with i from 2 to (its t's length) - 1
set x to item i of its t
set y to item (i - 1) of its t
set item i of its t to (its N) + (x's length) + y
end repeat
items 2 thru -2 of its t
end tell
end offset
offset of "happy" in stringA --> {8, 44, 83, 110}

View file

@ -0,0 +1,70 @@
-- offsets :: String -> String -> [Int]
on offsets(needle, haystack)
script match
property mx : length of haystack
property d : (length of needle) - 1
on |λ|(x, i, xs)
set z to d + i
mx z and needle = text i thru z of xs
end |λ|
end script
findIndices(match, haystack)
end offsets
-- TEST ---------------------------------------------------
on run
set txt to "I felt happy because I saw the others " & ¬
"were happy and because I knew I should " & ¬
"feel happy, but I wasnt really happy."
offsets("happy", txt)
--> {8, 44, 83, 110}
end run
-- GENERIC -------------------------------------------------
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set lng to length of xs
set acc to {}
tell mReturn(f)
repeat with i from 1 to lng
set acc to acc & (|λ|(item i of xs, i, xs))
end repeat
end tell
return acc
end concatMap
-- findIndices :: (a -> Bool) -> [a] -> [Int]
-- findIndices :: (String -> Bool) -> String -> [Int]
on findIndices(p, xs)
script go
property f : mReturn(p)
on |λ|(x, i, xs)
if f's |λ|(x, i, xs) then
{i}
else
{}
end if
end |λ|
end script
concatMap(go, xs)
end findIndices
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn