• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: An arbitrary if.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: An arbitrary if.


  • Subject: Re: An arbitrary if.
  • From: "Mark J. Reed" <email@hidden>
  • Date: Mon, 12 Sep 2011 18:04:00 -0400

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.
> 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
>
>



--
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

  • Follow-Ups:
    • Re: An arbitrary if.
      • From: Alex Zavatone <email@hidden>
    • Re: An arbitrary if.
      • From: "Mark J. Reed" <email@hidden>
References: 
 >An arbitrary if. (From: Alex Zavatone <email@hidden>)
 >Re: An arbitrary if. (From: Christopher Stone <email@hidden>)
 >Re: An arbitrary if. (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: An arbitrary if.
  • Next by Date: Re: An arbitrary if.
  • Previous by thread: Re: An arbitrary if.
  • Next by thread: Re: An arbitrary if.
  • Index(es):
    • Date
    • Thread