Re: An arbitrary if.
Re: An arbitrary if.
- Subject: Re: An arbitrary if.
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 12 Sep 2011 18:12:32 -0400
Footnote: you could of course do it all in a single reduce block; I was just trying to put together more generally useful pieces. e.g. Ruby:
x = "this is a cat"
[ "this", "cat" ].reduce(true) { |result, item|
result = result and x.include?(item)
}
The AS would then only require one script object:
script string_contains_all
property test_string: missing value
to invoke(accumulator, a_substring)
return (accumulator and (test_string contains a_substring))
end
end
set x to "this is a cat"
copy string_contains_all to x_contains_all
set test_string of x_contains_all to x
reduce( { "this", "cat" }, x_contains_all )
But that's still way too much boilerplate to solve the problem. :)
On Mon, Sep 12, 2011 at 6:04 PM, Mark J. Reed <
email@hidden> wrote:
> Thinking functionally, I would do something like this in e.g. Ruby:
>
> x = "this is a cat"
> [ "this", "cat" ].map { |word|
> x.include? word
> }.reduce(true) { |result, item|
> result = result and item
> }
>
> That is, first turn the list of words into a list of Boolean values
> indicating whether or not each word was found in the string, and then
> AND all those Booleans together.
>
> You can translate that into AppleScript using script objects in place
> of the blocks, but since script objects have to have names, the result
> is clunkier and harder to understand:
>
> to map(a_list, a_script)
> set result_list to {}
> repeat with an_item in a_list
> set end of result_list to a_script's invoke(get an_item)
> end
> return result_list
> end
>
> to reduce(a_list, initial_value, a_script)
> set accumulator to initial_value
> repeat with an_item in a_list
> set accumulator to a_script's invoke(accumulator, get an_item)
> end
> return accumulator
> end
>
> script ander
> to invoke(accumulator, an_item)
> return accumulator and an_item
> end
> end
>
> script string_contains
> property test_string: missing value
> to invoke(a_substring)
> return test_string contains a_substring
> end
> end
>
> set x to "this is a cat"
> copy string_contains to x_contains
> set x_contains's test_string to x
>
> reduce(map({"this", "cat"}, x_contains), true, ander)
>
> On Mon, Sep 12, 2011 at 5:38 PM, Christopher Stone
> <
email@hidden> wrote:
>> On Sep 12, 2011, at 16:07, Christopher Stone wrote:
>>
>> # Changing Luther's script just a bit to exit repeat on first NOT.
>>
>> ______________________________________________________________________
>> Hey Alex,
>> I didn't think the problem through when I looked at Luther's code. It is
>> not necessary to turn 'x' into a list; contains works perfectly well with
>> strings.
>> --
>> Best Regards,
>> Chris
>> --------------------------------
>> set x to "this is a cat"
>> set myList to {"this", "cat"}
>> set myResult to true
>> repeat with aword in myList
>> if not (x contains aword) then
>> set myResult to false
>> exit repeat
>> end if
>> end repeat
>> --------------------------------
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Archives:
http://lists.apple.com/archives/applescript-users>>
>> This email sent to
email@hidden
>>
>>
>
>
>
> --
> Mark J. Reed <
email@hidden>
>
--
Mark J. Reed <
email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden