• 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
Implementing a Map class in AS
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Implementing a Map class in AS


  • Subject: Implementing a Map class in AS
  • From: "Mark J. Reed" <email@hidden>
  • Date: Tue, 15 Apr 2008 22:37:28 -0400

I know this is a silly thing to do, but I was wondering if it was at
all idiomatic to use a script object as a class definition...

The script below lets me do things like this:

set myMap to Map's new()
myMap's store("someKey", "someValue")
myMap's fetch("someKey") -- returns someValue
myMap's unset("someKey")
myMap's fetch("someKey") -- returns ""

etc.


script Map
	property keyList : {}
	property valueList : {}

	on new()
		copy me to anInstance
		return anInstance
	end new

	on clear()
		set keyList to {}
		set valueList to {}
	end clear

	on findKey(theKey)
		if theKey is "" then
			return 0
		end if
		repeat with i from 1 to count (keyList)
			if item i of keyList is theKey then
				return i
			end if
		end repeat
		return 0
	end findKey

	on fetch(theKey)
		if theKey is "" then
			return ""
		end if
		set theIndex to my findKey(theKey)
		if theIndex > 0 then
			return item theIndex of valueList
		else
			return ""
		end if
	end fetch

	on store(theKey, theValue)
		set theIndex to my findKey(theKey)
		if theIndex > 0 then
			set item theIndex of valueList to theValue
		else
			set end of keyList to theKey
			set end of valueList to theValue
		end if
	end store

	on unset(theKey)
		set theIndex to my findKey(theKey)
		if theIndex > 0 then
			set item theIndex of keyList to ""
		end if
	end unset

	on keys()
		return keyList
	end keys

	on values()
		return valueList
	end values

end script




--
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: Implementing a Map class in AS
      • From: has <email@hidden>
  • Prev by Date: Re: Automation Sans Window Server
  • Next by Date: Re: Automation Sans Window Server
  • Previous by thread: Re: Finally - AppleScript 1-2-3 book
  • Next by thread: Re: Implementing a Map class in AS
  • Index(es):
    • Date
    • Thread