Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Option #7: undefined behavior.

Adbot
ADBOT LOVES YOU

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


leper khan posted:

Horror from the thread and all, but I never understood the appeal of option types. Doesn't give any more information than a pointer.

You don't solve the problem of having null data. And you potentially confer that problem on to _value_ types, which implies you don't understand how data enters your system.

Oh you have to check HasValue everywhere instead of != NULL. Wow great good job.

See here for a pretty good explanation.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


https://twitter.com/yiningkarlli/status/1736300787704426780

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


https://twitter.com/IroncladDev/status/1752349724105945181

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


code:
bool divides(int x, int y)
{
    return (int) (y / (float) x) * x == y;
}

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Bruegels Fuckbooks posted:

Here's a C# version that precomputes all even numbers up to IntMax, which obviously will save on calculation speed. We improve the initialization speed using a Lazy<T> so we only pay the cost of the precomputation on first use of IsEven()

code:
using System;
using System.Collections.Generic;

public static class MemoizedEvenChecker
{
    private static readonly Lazy<HashSet<int>> _evenNumbers = new Lazy<HashSet<int>>(() =>
    {
        var numbers = new HashSet<int>();
        for (int i = 0; i <= int.MaxValue; i += 2)
        {
            numbers.Add(i);
        }
        return numbers;
    });

    public static bool IsEven(int number)
    {
        return _evenNumbers.Value.Contains(number);
    }
}

It'd be even more efficient to move the precomputation to compile time.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


I briefly looked into generating a lookup table using templates but that's way too much effort for me.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


I could write a lot about R here but this latest example I came across is a complete mystery:

code:
f <- function() { for (i in 1:1) { i } }
f() # returns NULL

g <- function() { i <- 1; if (i == 1) { i } }
g() # returns 1


The convention for function calls in R is that they return the value of the last expression evaluated. That sure looks like it should be 1 in both cases but that's not what happens. I'm probably happier not thinking too much about why.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


https://twitter.com/ryxcommar/status/1772105348872011972

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


csammis posted:

What is a database but coding horrors made persistent

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


https://twitter.com/the_moisrex/status/1776661987855941776

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


You can get weird behavior even with machine integers. The function that sends x to -x has two fixed points and that means you can't always assume that abs(x) > 0.

Adbot
ADBOT LOVES YOU

ultrafilter
Aug 23, 2007

It's okay if you have any questions.



  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply