optional arguments in haskell - haskell

I have declared my own type:
data Book = Bookinfo {
bookId :: Int,
title :: String
} deriving(Show)
and now:
x = Bookinfo
it is all ok, valid statement
but making bookId x throws an error.
If I would be able to handle errors in Haskell that would be ok but right now I cant do this So Im curious how to make not specified values of fields take default value, and what exactly value is there when I'm not giving vcalues of fields in construcotr ?
thanks for help

-- this one is not a Book but actually a function that can make one:
alternativeCtrFunc = Bookinfo
defaultBook = Bookinfo { bookId = 3, title = "Welcome to the Monkey House" }
x = defaultBook
y = defaultBook { bookId = 7 }
z = defaultBook { title = "The Cider House Rules" }

Related

What is a alternative for python classes in lua?

So i'm trying to convert python to lua but a road block i have right now is the class i'm not sure how i would convert a class to lua does anyone know how i would go about converting a class from python to lua?
python class
class
The OOP magic can be done with Lua tables and metatables.
Python
Look at The __init__() Function example here...
https://www.w3schools.com/python/python_classes.asp
Lets implement this with Lua...
( Lua 5.4 interactive console )
> _VERSION
Lua 5.4
> Person = setmetatable({},{
__call = function(self, name, age)
self.name = name
self.age = age
return {name = self.name, age = self.age}
end
})
> p1 = Person("John", 36)
> print(p1.name, p1.age)
John 36
So, some readers would ask: Why a metatabled table?
A simple function could do the same job.
Once a table has a metatable it is quit simple to add methods...
> Person = setmetatable({},{
__call = function(self, name, age)
self.name, self.age = name, age
table.insert(self, {name = self.name, age = self.age}) -- Numbered keys are the Data part
return {name = self.name, age = self.age}
end,
__index = {list = function(self) for i = 1, #self do print(self[i].name, self[i].age) end end}
})
> Person("John", 31);
> Person("Jack", 32);
> Person("Jim", 33);
> Person:list()
John 31
Jack 32
Jim 33
...that can handle the data of the table by itself.
Its a great benefit that the behaviour of a table can be controlled with metatables/methods.
And above example is only the tip of the iceberg.
Have a look at the __index of a string...
> for key, value in pairs(getmetatable(_VERSION).__index) do print(key, "=", value) end
byte = function: 0x565d6f20
lower = function: 0x565d4d90
len = function: 0x565d4750
sub = function: 0x565d7210
dump = function: 0x565d5d00
gsub = function: 0x565d7dc0
char = function: 0x565d5060
unpack = function: 0x565d6530
match = function: 0x565d7da0
packsize = function: 0x565d6420
pack = function: 0x565d6950
upper = function: 0x565d4ac0
format = function: 0x565d52d0
reverse = function: 0x565d4b50
find = function: 0x565d7db0
gmatch = function: 0x565d70d0
rep = function: 0x565d4be0
Therefore this is possible...
> print(("koyaanisqatsi"):upper():reverse())
ISTAQSINAAYOK
Means: Methods can be chained if their returning datatype has a method
Exceptions are: len() accept but dont return a string and sub() needs numbers before it returns a string also dump() needs a self defined function and returns a binary (almost unreadable) string.

Kotlin getString() exepects an Int? Why?

I'm going through https://developer.android.com/codelabs/basic-android-kotlin-training-project-lemonade. Here, while trying to make "else" do nothing, I've stumbled upon "Type mismatch" errors.
Regarding setting the view elements, this is what I've come up with so far:
private fun setViewElements() {
val textAction: TextView = findViewById(R.id.text_action)
val text = when (lemonadeState) {
SELECT -> R.string.lemon_select
SQUEEZE -> R.string.lemon_squeeze
DRINK -> R.string.lemon_drink
RESTART -> R.string.lemon_empty_glass
else -> null
}
textAction.text = getString(text)
I've tried all workarounds for "doing nothing": {}, Unit, null, empty string (and even data types like String, Int, Double...) but all I get is a "Type mismatch: inferred type is Any but Int was expected" (or "...inferred type is Int?...") error. So much so that an Int does makes the error disapear, as in:
...
else -> 0
According to the docs, both versions of getString(), single and double paramater, work stritcly with strings: parameter(s) and return type. So why on Earth is it saying this function expects an Int?
Also, writing text as an instance of Int (text: Int), doesn't affect anything, meaning it is in fact an Int. I am missing something big here: aren't those R.string.<name> supposed to be strings?
Btw, I did try this:
private fun setViewElements() {
val textAction: TextView = findViewById(R.id.text_action)
val text = when (lemonadeState) {
SELECT -> R.string.lemon_select
SQUEEZE -> R.string.lemon_squeeze
DRINK -> R.string.lemon_drink
else -> R.string.lemon_empty_glass
}
textAction.text = getString(text)
which is errorless and looks better. However, I wanted to keep the specificity, if possible (and it doesn't answer my question).
As the comments pointed out, getString takes a Int, which is a resource ID. This identifies one of the strings you have written in your XML files. This way, you can easily have e.g. multiple versions of the string, one for each localisation for your app, and getString will figure out which string to use based on the device locale. R.string.lemon_select, R.string.lemon_squeeze etc are those identifiers.
One way to do nothing, is to return just evaluate to null - you just have to handle the null value yourself. This will cause text to be of type Int?.
val text = when (lemonadeState) {
SELECT -> R.string.lemon_select
SQUEEZE -> R.string.lemon_squeeze
DRINK -> R.string.lemon_drink
RESTART -> R.string.lemon_empty_glass
else -> null
}
if (text != null) {
textAction.text = getString(text)
}
From your last code snippet though, it seems like you want to set it to lemon_empty_glass when the else branch is reached. If that is the case, then you can do:
val text = when (lemonadeState) {
SELECT -> R.string.lemon_select
SQUEEZE -> R.string.lemon_squeeze
DRINK -> R.string.lemon_drink
RESTART -> R.string.lemon_empty_glass
else -> null
}
textAction.text = getString(text ?: R.string.lemon_empty_glass)

How to update a Data attribute defined with Maybe

I'm playing with Haskell using https://hackage.haskell.org/package/cursor library. I have this data definition:
data TuidoState =
TuidoState { tuidoStateEntries :: Maybe (NonEmptyCursor Entry) }
And I have this function:
buildNewItem :: TuidoState -> TuidoState
buildNewItem s =
let nextID = 10 -- TODO update here to function to return ID
headerTitle = "Test new item"
newEntry = Entry { entryHeader= Header { headerTitle= headerTitle }
, entryBody= Just (Body { bodyTitle= headerTitle })
, entryTags= [Tag {tagName= headerTitle}]
}
actualEntries = (tuidoStateEntries s)
ne = NE.nonEmpty [newEntry]
in
case actualEntries of
Nothing ->
s { tuidoStateEntries = Just(makeNonEmptyCursor ne) }
Just value -> s { tuidoStateEntries = Just(value) } -- possible here I will want to just add the new Entry to the existing list
But, I cannot understand the error:
• Couldn't match expected type ‘NE.NonEmpty Entry’
with actual type ‘Maybe (NE.NonEmpty Entry)’
• In the first argument of ‘makeNonEmptyCursor’, namely ‘ne’
In the first argument of ‘Just’, namely ‘(makeNonEmptyCursor ne)’
In the ‘tuidoStateEntries’ field of a record
|
327 | s { tuidoStateEntries = Just(makeNonEmptyCursor ne) }
Could someone help me with it?
nonEmpty takes an arbitrary list, and so cannot guarantee it returns a non-empty list. Instead, it returns a Maybe (NonEmpty a) to indicate that it may either return a nonempty list or cause an error.
Consider using NonEmpty's constructor, (:|), directly instead.
ne = newEntry NE.:| []

Avoiding primitive obsession in Haskell

From http://learnyouahaskell.com/making-our-own-types-and-typeclasses
data Person = Person { name :: String
, age :: Int
} deriving (Show)
In a real application, using primitives like String and Int for name and age would constitue primitive obsession, a code smell. (also obviously Date born is preferable to Int age but let's ignore that) Instead, one would prefer something like
newtype Person = Person { name :: Name
, age :: Age
} deriving (Show)
In an OO language this would look something like
class Person {
Name name;
Age age;
Person(Name name, Age age){
if (name == null || age == null)
throw IllegalArgumentException();
this.name = name;
this.age = age;
}
}
class Name extends String {
Name(String name){
if (name == null || name.isEmpty() || name.length() > 100)
throw IllegalArgumentException();
super(name);
}
}
class Age extends Integer {
Age(Integer age){
if (age == null || age < 0)
throw IllegalArgumentException();
super(age);
}
}
But how is the same achieved in idiomatic, best practice Haskell?
Make Name abstract and provide a smart constructor. This means that you do not export the Name data constructor, and provide a Maybe-returning constructor instead:
module Data.Name
( Name -- note: only export type, not data constructor
, fromString
, toString
) where
newtype Name = Name String
fromString :: String -> Maybe Name
fromString n | null n = Nothing
| length n > 100 = Nothing
| otherwise = Just (Name n)
toString :: Name -> String
toString (Name n) = n
It is now impossible to construct a Name value of the wrong length outside of this module.
For Age, you could do the same thing, or use a type from Data.Word, or use the following inefficient but guaranteed non-negative representation:
data Age = Zero | PlusOne Age
This may be code smell in some languages, but it's not usually considered one in Haskell. You have to choose a concrete representation of a name and birth date somewhere, and the datatype declaration of Person is probably the best place to do it. In Haskell, the usual way to keep other code from depending on the name representation would be to make Person abstract. Instead of exposing the Person constructor, expose functions for creating, modifying, and inspecting Person values.

Swift String.removeRange cannot compile

I don't understand what to do with the issue reported by the compiler. I tried to create a Range, but it says Index is not known:
//let range = matches.first!.range.location
let range = Range(
start:matches.first!.range.location,
end: matches.first!.range.location+matches.first!.range.length
)
id = text[range]
var t = text
t.removeRange(range)
return t
Compiler says: Cannot invoke 'removeRange' with an argument list of type '(Range)' on t.removeRange(range).
I'm pretty sure it's evident, but I lost a great deal of time on such a small issue… any help highly appreciated!
As your error says that:
Cannot invoke 'removeRange' with an argument list of type '(Range)'
Means there is a problem with your range instance type and removeRange function will only accept an argument with type Range<String.Index> and its syntax is :
/// Remove the indicated `subRange` of characters
///
/// Invalidates all indices with respect to `self`.
///
/// Complexity: O(\ `count(self)`\ ).
mutating func removeRange(subRange: Range<String.Index>)
And here is working example with removeRange:
var welcome = "hello there"
let range = advance(welcome.endIndex, -6)..<welcome.endIndex
welcome.removeRange(range)
println(welcome) //hello
Hope this will help.
Swift 2.2 example of removing first 4 characters:
let range = text.startIndex..<text.startIndex.advancedBy(4)
text.removeRange(range)
That first line feels verbose. I hope newer Swift versions improve upon it.
Here is the working equivalent snippet:
static func unitTest() {
let text = "a👿bbbbb🇩🇪c"
let tag = Tag(id: "🇩🇪")
tag.regex = "👿b+"
print ("Unit test tag.foundIn(\(text)) ? = \(tag.foundIn(text))")
}
func foundIn(text: String) -> (id:String, remainingText:String)? {
// if a regex is provided, use it to capture, and keep the capture as a tag ID
if let regex = regex {
let r = Regex(regex) // text =~ regex
let matches = r.matches(text)
if matches.count >= 1 {
let first = matches.first!.range
let start = advance(text.startIndex, first.location)
let end = advance(start, first.length-1)
let range = Range(start: start, end: end)
id = text[range]
var t = text
t.removeRange(range)
return (id, t)
}
return nil
}
else if let range = text.rangeOfString(id) {
var t = text
t.removeRange(range)
return (id, t)
}
else {
return nil
}
}
The unit test returns :
Unit test tag.foundIn(a👿bbbbb🇩🇪c) ? = Optional(("👿bbbbb", "a🇩🇪c"))

Resources