Re: [elm-discuss] Re: Divide by zero?

2017-02-18 Thread 'Rupert Smith' via Elm Discuss
On Sunday, February 19, 2017 at 12:09:18 AM UTC, Alex Barry wrote:
>
> But that if statement would only go around something doing integer 
> division, where the divisor could be zero. What you're suggesting would be 
> required for at least all integer division, and potentially a pattern for 
> other integer operations. That's a huge impact.
>

Good point.

divideByFive x = x // 5 -- I know 5 is not zero.

Elm does have runtime exceptions then. It seems like a bug that // 0 give 
0, the point of the type system is to help eliminate errors, but this just 
lets an error slip through potentially un-noticed.

Would it not be better to make // 0 equivalent to Debug.crash "divide by 
zero"?

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Is type inference in Elm fully decideable?

2017-02-18 Thread 'Rupert Smith' via Elm Discuss
On Wednesday, February 15, 2017 at 8:49:13 PM UTC, Rupert Smith wrote:
>
> I am trying to build a model with states, such that fields are only 
> available that are actually needed in the relevant state. The model is like 
> this:
>
> type alias WithPosition a =
> { a | rect : Rectangle }
>

>
> type alias WithValue a =
> { a | value : String }
>
>
> type State
> = Hidden
> | Aware (WithPosition {})
> | Active (WithPosition (WithValue {}))
> | Inactive (WithPosition (WithValue {}))
>

Having played around with this for a while, I don't think this pattern is 
worth continuing with.

It is much easier to use non-extensible records and take advantage of 
tagged union constructors letting you have >1 argument. Like this:

type State
= Hidden
| Aware WithPosition
| Active WithPosition WithValue
| Inactive WithPosition

(Of course I could just use Rectangle and String directly - but the actual 
model I am working with has more fields)

This avoids the need to unpack and repack record types unnecessarily to try 
and work around the typing issues.

It has been a learning experience to play around with them, but I think 
without an existential qualifier, extensible records are not terribly 
useful at all - except for the simple case where you want to write a 
function that projects the fields of a record onto a sub set. As 
existential qualifier would break type inference and that is too useful - 
so extensible records are best avoided.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Divide by zero?

2017-02-18 Thread Alex Barry
But that if statement would only go around something doing integer
division, where the divisor could be zero. What you're suggesting would be
required for at least all integer division, and potentially a pattern for
other integer operations. That's a huge impact.

On Feb 18, 2017 7:06 PM, "'Rupert Smith' via Elm Discuss" <
elm-discuss@googlegroups.com> wrote:

On Saturday, February 18, 2017 at 11:23:01 PM UTC, Alex Barry wrote:
>
> I think for integer division, it has to return 0 because infinity is
> expressed as a float. I'm going to agree with Erkal, though, you definitely
> don't want to wrap all your math statements in a maybe or result type, that
> would make most code considerably more verbose.
>

But to use it safely you have to wrap it in a check for zero anyway

if divisor == 0 then
  ...
else
  val // divisor

Which is equally complex as:

case (a // b) of
  Integer val -> val
  DivideByZero -> ...


-- 
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Divide by zero?

2017-02-18 Thread 'Rupert Smith' via Elm Discuss
On Saturday, February 18, 2017 at 11:23:01 PM UTC, Alex Barry wrote:
>
> I think for integer division, it has to return 0 because infinity is 
> expressed as a float. I'm going to agree with Erkal, though, you definitely 
> don't want to wrap all your math statements in a maybe or result type, that 
> would make most code considerably more verbose.
>

But to use it safely you have to wrap it in a check for zero anyway

if divisor == 0 then
  ...
else
  val // divisor

Which is equally complex as:

case (a // b) of
  Integer val -> val
  DivideByZero -> ...


-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Divide by zero?

2017-02-18 Thread Alex Barry
I think for integer division, it has to return 0 because infinity is
expressed as a float. I'm going to agree with Erkal, though, you definitely
don't want to wrap all your math statements in a maybe or result type, that
would make most code considerably more verbose.

On Sat, Feb 18, 2017 at 5:38 PM, 'Rupert Smith' via Elm Discuss <
elm-discuss@googlegroups.com> wrote:

> On Saturday, February 18, 2017 at 9:53:49 PM UTC, Erkal Selman wrote:
>>
>> Why didn't you try it?
>> Try it!
>> Yes, it returns Infinity.
>> Would you that error handling for every other operation, like for example
>> the squareroot of negative numbers, or the logarithm of zero? I don't think
>> that this is a good idea for a language like elm.
>>
>
> With elm-repl:
>
> > type alias T = { val : Int }
> > t = T 4
> { val = 4 } : Repl.T
> > x = t.val / 0
> -- TYPE MISMATCH -
> repl-temp-000.elm
>
> The left argument of (/) is causing a type mismatch.
>
> 8| t.val / 0
>^
> (/) is expecting the left argument to be a:
>
> Float
>
> But the left argument is:
>
> Int
>
> Hint: Elm does not automatically convert between Ints and Floats. Use
> `toFloat`
> and `round` to do specific conversions.
> 
>
> Hint: The (/) operator is specifically for floating point division, and
> (//) is
> for integer division. You may need to do some conversions between ints and
> floats to get both arguments matching the division operator you want.
>
>
> > x = t.val // 0
> 0 : Int
> >
>
> So for integer division by zero, it simply returns zero!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Divide by zero?

2017-02-18 Thread 'Rupert Smith' via Elm Discuss
On Saturday, February 18, 2017 at 9:53:49 PM UTC, Erkal Selman wrote:
>
> Why didn't you try it? 
> Try it! 
> Yes, it returns Infinity.
> Would you that error handling for every other operation, like for example 
> the squareroot of negative numbers, or the logarithm of zero? I don't think 
> that this is a good idea for a language like elm.
>

With elm-repl:

> type alias T = { val : Int }
> t = T 4
{ val = 4 } : Repl.T
> x = t.val / 0
-- TYPE MISMATCH - 
repl-temp-000.elm

The left argument of (/) is causing a type mismatch.

8| t.val / 0
   ^
(/) is expecting the left argument to be a:

Float

But the left argument is:

Int

Hint: Elm does not automatically convert between Ints and Floats. Use 
`toFloat`
and `round` to do specific conversions.


Hint: The (/) operator is specifically for floating point division, and 
(//) is
for integer division. You may need to do some conversions between ints and
floats to get both arguments matching the division operator you want.


> x = t.val // 0
0 : Int
> 

So for integer division by zero, it simply returns zero!

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Divide by zero?

2017-02-18 Thread Erkal Selman
Why didn't you try it? 
Try it! 
Yes, it returns Infinity.
Would you that error handling for every other operation, like for example 
the squareroot of negative numbers, or the logarithm of zero? I don't think 
that this is a good idea for a language like elm.


On Saturday, February 18, 2017 at 4:59:01 PM UTC+1, Rupert Smith wrote:
>
> Elm has no runtime exceptions - which is really cool, the type system 
> prevents me from coding things that can fail at runtime. But what about 
> integer division by zero?
>
> I have not tried it, but as Ints are translated as numbers in the 
> javavscript, I think diving by zero will return me a floating point 
> Infinity, yes?
>
> Should division force you to deal with failures like this
>
> case (a / b) of
>   Integer val -> ...
>   DivideByZero -> ...
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Polymorphic components

2017-02-18 Thread art yerkes
Most languages have some form of runtime dispatch but in elm you have to 
write it yourself, so it's up to you to choose a dispatch model.

A couple of patterns for this:

https://medium.com/@debois/elm-components-3d9c00c6c612#.hht7utwro
https://medium.com/@prozacchiwawa/the-im-stupid-elm-language-nugget-13-84ba8539ebc4#.xys1owh5e

On Saturday, February 18, 2017 at 3:20:59 AM UTC-8, Steffen wrote:
>
> To avoid the XY pitfall, here's some more context:
>
> I'm building a web scraper. It scans a number of websites for a fixed set 
> of data which is the same for all sites. The "driver" to access the sites 
> is site-specific and carries some state.
>
> The model looks like this:
>
>
> type SiteDriver
> = Site1Driver Site1.Model
> | Site2Driver Site2.Model
>
>
> type alias SiteModel =
> { name : String
> , datum1 : String
> , datum2 : Int
> , enabled : Bool
> , driver : SiteDriver
> }
>
>
> init : List SiteModel
> init =
> [ { name = "Site 1"
>   , datum1 = ""
>   , datum2 = 0
>   , enabled = True
>   , driver = Site1Driver Site1.init
>   }
> , { name = "Site 2"
>   , datum1 = ""
>   , datum2 = 0
>   , enabled = True
>   , driver = Site2Driver Site2.init
>   }
> ]
>
>
> The list of SiteModels is fixed, they are just enabled or disabled as 
> necessary. This approach works fine for rendering HTML without code 
> duplication but I can't get updates to work...
>
> Thanks for watching.
>
> Sincerely,
> Steffen
>
>
>
> On Saturday, 18 February 2017 04:57:45 UTC+1, Steffen wrote:
>>
>> Hello,
>>
>> I'm new to Elm and stuck with a problem. In my model I have a list of 
>> components of different types. Now I'm trying to get the view and update 
>> functions right.
>>
>> This has surely already been solved, but I didn't find a solution 
>> anywhere...
>>
>> Minimal example:
>>
>> https://ellie-app.com/qsQjTYcs2va1/3
>>
>> Uncomment line 166 to see what I mean. I would need to "cast" 
>> ComponentMessage and -Model to their Int subtypes.
>>
>> What did I get wrong?
>>
>> Sincerely,
>> Steffen
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Divide by zero?

2017-02-18 Thread 'Rupert Smith' via Elm Discuss
Elm has no runtime exceptions - which is really cool, the type system 
prevents me from coding things that can fail at runtime. But what about 
integer division by zero?

I have not tried it, but as Ints are translated as numbers in the 
javavscript, I think diving by zero will return me a floating point 
Infinity, yes?

Should division force you to deal with failures like this

case (a / b) of
  Integer val -> ...
  DivideByZero -> ...

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Fancy date pickers (or other widgets) with no ports/subscriptions: my solution.

2017-02-18 Thread Witold Szczerba
Hi,
last week I was introducing Elm into one of the internal apps at my work. I
have used Date Range Picker www.daterangepicker.com. For those who do not
read Reddit, here's the demo app and short description:
https://www.reddit.com/r/elm/comments/5uqa13/those_fancy_date_pickers_in_elm_watch_this_no/
I hope someone will find it useful.

Regards,
Witold Szczerba

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Polymorphic components

2017-02-18 Thread Steffen
To avoid the XY pitfall, here's some more context:

I'm building a web scraper. It scans a number of websites for a fixed set 
of data which is the same for all sites. The "driver" to access the sites 
is site-specific and carries some state.

The model looks like this:


type SiteDriver
= Site1Driver Site1.Model
| Site2Driver Site2.Model


type alias SiteModel =
{ name : String
, datum1 : String
, datum2 : Int
, enabled : Bool
, driver : SiteDriver
}


init : List SiteModel
init =
[ { name = "Site 1"
  , datum1 = ""
  , datum2 = 0
  , enabled = True
  , driver = Site1Driver Site1.init
  }
, { name = "Site 2"
  , datum1 = ""
  , datum2 = 0
  , enabled = True
  , driver = Site2Driver Site2.init
  }
]


The list of SiteModels is fixed, they are just enabled or disabled as 
necessary. This approach works fine for rendering HTML without code 
duplication but I can't get updates to work...

Thanks for watching.

Sincerely,
Steffen



On Saturday, 18 February 2017 04:57:45 UTC+1, Steffen wrote:
>
> Hello,
>
> I'm new to Elm and stuck with a problem. In my model I have a list of 
> components of different types. Now I'm trying to get the view and update 
> functions right.
>
> This has surely already been solved, but I didn't find a solution 
> anywhere...
>
> Minimal example:
>
> https://ellie-app.com/qsQjTYcs2va1/3
>
> Uncomment line 166 to see what I mean. I would need to "cast" 
> ComponentMessage and -Model to their Int subtypes.
>
> What did I get wrong?
>
> Sincerely,
> Steffen
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Is a decoder the right level of abstraction for parsing JSON?

2017-02-18 Thread Kasey Speakman
An update. I'm using the ports approach to deal with JSON (and also sending 
the HTTP request). For several months, I've had a small but critical app in 
production using that. Another project in development is too. In the 
process, I have run across two additional caveats with this approach.

   1. *Ports don't convert undefined properties to Maybe.Nothing.* It's an 
   open issue from Jan 2016. 
   For a Maybe property, the JSON must have the property present and set to 
   null. Otherwise error. This is particularly annoying when you store the 
   data as JSON and pass it back to the client as-is. To work around this 
   issue, I either have to waste space storing nulls in the database or waste 
   (CPU/response) time server-side to inject nulls in the response.
   
   2. *Cmd.map can't be used with this method.*
   Using Http module, you can use Cmd.map to take some data from the 
   request and give it to the response Msg. Using ports, you can't do that. 
   I've noticed this when the data is easy to provide for a request, but after 
   the response comes back it is less convenient to dig out of the model (e.g. 
   behind a case statement).

Neither of these are blockers for me, just nuisance issues. It still beats 
maintaining codecs.

I've seen rumblings about tools for code-gen'ing JSON codecs for us (maybe 
elm-format?  There also 
exists elm-swagger, but I don't use swagger.). I dunno though. Where 
possible, I tend to avoid code-gen because it's for handling a really 
tedious problem. And if the code-gen fails, then I have to handle a really 
tedious problem. (XSD/WSDL flashbacks.)

All it would really take for a profound QoL improvement are a couple of 
"special" functions on Http that handle data exactly like ports do now... 
just saying.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.