Codex NG
Recurrent Flamer
- Joined
- Jul 24, 2015
- Messages
- 2,994
- Solutions
- 12
- Reaction score
- 1,657
Might not seem important, I like to sometimes write little utility functions although I don't always use them.
This is a simple function (if it doesn't already exist) which will allow you to state where the word a (yes a is a word) or an will be used in a sentence.
Unfortunately this only pertains to english, I am sure you can adapt it to your native language based on its rules of grammar.
If you don't have an isInArray function in your distro or whatever you are working on, you can always use this function definition below.
So how can this be used.. Lets take a look at an example
Lets try another
One more
Its not perfect by any means, but its definitely helpful to have functions which can simplify things at times.
This is a simple function (if it doesn't already exist) which will allow you to state where the word a (yes a is a word) or an will be used in a sentence.
Unfortunately this only pertains to english, I am sure you can adapt it to your native language based on its rules of grammar.
Code:
--[[
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
Code:
function aOrAn(word)
local vowels = {'a', 'e', 'i', 'o', 'u', 'x'}
return isInArray(vowels, word:sub(1, 1):lower()) and ' an '..word or ' a '..word
end
If you don't have an isInArray function in your distro or whatever you are working on, you can always use this function definition below.
Code:
function isInArray(a, x)
for k, v in pairs(a) do
if v == x then
return true
end
end
return false
end
So how can this be used.. Lets take a look at an example
Code:
function aOrAn(word)
local vowels = {'a', 'e', 'i', 'o', 'u', 'x'}
return isInArray(vowels, word:sub(1, 1):lower()) and ' an '..word or ' a '..word
end
local name = 'elephant'
print("You have summoned"..aOrAn(name)..".")
-- output
You have summoned an elephant.
Lets try another
Code:
local name = 'deer'
print("You have summoned"..aOrAn(name)..".")
-- output
You have summoned a deer.
One more
Code:
local name = 'Xray'
print("You went and got"..aOrAn(name)..".")
-- output
You went and got an Xray.
Its not perfect by any means, but its definitely helpful to have functions which can simplify things at times.
Last edited: