- Orzo
- Sep 3, 2004
-
IT! IT is confusing! Say your goddamn pronouns!
|
what are the comments like, is anyone a member?
|
#
?
May 21, 2011 17:25
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
?
Apr 28, 2024 14:51
|
|
- tef
- May 30, 2004
-
-> some l-system crap ->
|
qntm posted:
So I guess the real challenge overcome here was to slightly obfuscate the location of the "if" statement!
this is what some java programmers think OO is actually about
|
#
?
May 21, 2011 18:39
|
|
- TasteMyHouse
- Dec 21, 2006
-
|
That Stupid Article posted:Use only one dot per line. This step prevents you from reaching deeply into other objects to get at fields or methods, and thereby conceptually breaking encapsulation.
I'd like to see his definition of encapsulation for which this statement makes any sense at all.
|
#
?
May 21, 2011 21:33
|
|
- Orzo
- Sep 3, 2004
-
IT! IT is confusing! Say your goddamn pronouns!
|
TasteMyHouse posted:
I'd like to see his definition of encapsulation for which this statement makes any sense at all.
Actually that point is pretty valid, a pattern of calls like thisObject.getSubObject().getOtherSubObject().performAction() is generally pretty bad, and it violates the conventional definition of encapsulation because objects aren't hiding their internals at all.
|
#
?
May 21, 2011 21:47
|
|
- TasteMyHouse
- Dec 21, 2006
-
|
Orzo posted:
Actually that point is pretty valid, a pattern of calls like thisObject.getSubObject().getOtherSubObject().performAction() is generally pretty bad, and it violates the conventional definition of encapsulation because objects aren't hiding their internals at all.
I agree with that, but thats a result of having those methods that expose the subobjects in the first place, and isn't inherent in chaining method calls by using multiple dots per line.
|
#
?
May 21, 2011 22:11
|
|
- trex eaterofcadrs
- Jun 17, 2005
-
My lack of understanding is only exceeded by my lack of concern.
|
TasteMyHouse posted:
I agree with that, but thats a result of having those methods that expose the subobjects in the first place, and isn't inherent in chaining method calls by using multiple dots per line.
That's why he should have said "don't traverse object graphs" instead of some bs pseudo-wisdom that can't withstand a moment of scrutiny.
|
#
?
May 21, 2011 22:20
|
|
- Sedro
- Dec 31, 2008
-
|
More like
code:SubObject sub1 = thisObject.getSubObject();
if (sub1 != null)
{
OtherSubObject sub2 = sub1.getOtherSubObject();
if (sub2 != null)
{
OtherOtherSubObject sub3 = sub2.getOtherOtherSubObject();
if (sub3 != null)
{
...
sub23623456.performAction();
...
}
}
}
Java 7 has new operators to make this crap faster to write.
|
#
?
May 22, 2011 04:14
|
|
- TOO SCSI FOR MY CAT
- Oct 12, 2008
-
this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
|
It's me, I'm the horror
Found this while updating an old library; it's is a Haskell function I wrote for parsing DBus signatures. It's called a lot, so speed is important, but...well, the spec just isn't that complex:
code:type = atom | container
atom = 'y' | 'b' | 'n' | 'q' | 'i' | 'u' | 'x' | 't' | 'd' | 's' | 'o' | 'g'
container = 'v' | array | dict | struct
array = 'a' type
dict = 'a{' atom '}'
struct = '(' type+ ')'
avert your eyes, ye sensitive:
code:mkBytesSignature :: ByteString -> Maybe Signature
mkBytesSignature = unsafePerformIO . flip unsafeUseAsCStringLen io where
io (cstr, len) = case len of
0 -> return $ Just $ Signature []
1 -> fmap fast $ peek cstr
_ | len <= 255 -> slow (castPtr cstr) len
_ -> return Nothing
fast c = parseAtom c (\t -> Just (Signature [t])) $ case c of
0x76 -> Just (Signature [DBusVariant])
_ -> Nothing
parseAtom c yes no = case c of
0x62 -> yes DBusBoolean
0x79 -> yes DBusByte
0x6E -> yes DBusInt16
0x69 -> yes DBusInt32
0x78 -> yes DBusInt64
0x71 -> yes DBusWord16
0x75 -> yes DBusWord32
0x74 -> yes DBusWord64
0x64 -> yes DBusDouble
0x73 -> yes DBusString
0x67 -> yes DBusSignature
0x6F -> yes DBusObjectPath
_ -> no
slow buf len = loop [] 0 where
loop acc ii | ii >= len = return . Just . Signature $ reverse acc
loop acc ii = do
c <- peekElemOff buf ii
let next t = loop (t : acc) (ii + 1)
parseAtom c next $ case c of
0x76 -> next DBusVariant
-- '('
0x28 -> do
mt <- structure buf len (ii + 1)
case mt of
Just (ii', t) -> loop (t : acc) ii'
Nothing -> return Nothing
-- 'a'
0x61 -> do
mt <- array buf len (ii + 1)
case mt of
Just (ii', t) -> loop (t : acc) ii'
Nothing -> return Nothing
_ -> return Nothing
structure buf len = loop [] where
loop _ ii | ii >= len = return Nothing
loop acc ii = do
c <- peekElemOff buf ii
let next t = loop (t : acc) (ii + 1)
parseAtom c next $ case c of
0x76 -> next DBusVariant
-- '('
0x28 -> do
mt <- structure buf len (ii + 1)
case mt of
Just (ii', t) -> loop (t : acc) ii'
Nothing -> return Nothing
-- ')'
0x29 -> return $ Just $ (ii + 1, DBusStructure (reverse acc))
-- 'a'
0x61 -> do
mt <- array buf len (ii + 1)
case mt of
Just (ii', t) -> loop (t : acc) ii'
Nothing -> return Nothing
_ -> return Nothing
array _ len ii | ii >= len = return Nothing
array buf len ii = do
c <- peekElemOff buf ii
let next t = return $ Just (ii + 1, DBusArray t)
parseAtom c next $ case c of
0x76 -> next DBusVariant
-- '('
0x28 -> do
mt <- structure buf len (ii + 1)
case mt of
Just (ii', t) -> return $ Just (ii', DBusArray t)
Nothing -> return Nothing
-- '{'
0x7B -> dict buf len (ii + 1)
-- 'a'
0x61 -> do
mt <- array buf len (ii + 1)
case mt of
Just (ii', t) -> return $ Just (ii', DBusArray t)
Nothing -> return Nothing
_ -> return Nothing
dict _ len ii | ii + 1 >= len = return Nothing
dict buf len ii = do
c1 <- peekElemOff buf ii
c2 <- peekElemOff buf (ii + 1)
let mt1 = parseAtom c1 Just Nothing
let next t = return $ Just (ii + 2, t)
mt2 <- parseAtom c2 next $ case c2 of
0x76 -> next DBusVariant
-- '('
0x28 -> structure buf len (ii + 2)
-- 'a'
0x61 -> array buf len (ii + 2)
_ -> return Nothing
case mt2 of
Nothing -> return Nothing
Just (ii', t2) -> if ii' >= len
then return Nothing
else do
c3 <- peekElemOff buf ii'
return $ do
if c3 == 0x7D then Just () else Nothing
t1 <- mt1
Just (ii' + 1, DBusDictionary t1 t2)
|
#
?
May 22, 2011 04:35
|
|
- Che Delilas
- Nov 23, 2009
-
FREE TIBET WEED
|
Geekner posted:
code:ohMyGod?[0]?.pleaseKill(me);
It's fitting that they all have question marks as part of the operator, because people looking at lines like that are going to be thinking "what the gently caress is this poo poo?"
|
#
?
May 22, 2011 05:35
|
|
- Sedro
- Dec 31, 2008
-
|
Geekner posted:
code:ohMyGod?[0]?.pleaseKill(me);
What if that array is empty? Surely the indexer should return null instead of throwing an exception.
|
#
?
May 22, 2011 06:22
|
|
- zeekner
- Jul 14, 2007
-
|
Sedro posted:
What if that array is empty? Surely the indexer should return null instead of throwing an exception.
From what I understand, if the left side of the ? is null, it will return null instead of attempting to read the array. The second question mark does the same for the array value.
code:if(ohMyGod != null && ohMyGod[0] != null){
Object whatever = ohMyGod.pleaseKill(me);
}else{
Object whatever = null;
}
Or for extra horror:
code:Object whatever = (ohMyGod != null ? (ohMyGod[0] != null ? ohMyGod.pleaseKill(me) : null) : null);
Kill me.
e: Misread that, from that link: "(the array) is non-null and non-empty", it'll return null instead of throwing an error.
zeekner fucked around with this message at 07:01 on May 22, 2011
|
#
?
May 22, 2011 06:54
|
|
- Sedro
- Dec 31, 2008
-
|
The operators won't perform bounds checking on your array so there is still the possibility of error.
What we really need is the tried and true onErrorResumeNext()
|
#
?
May 22, 2011 07:07
|
|
- wellwhoopdedooo
- Nov 23, 2007
-
Pound Trooper!
|
Am I missing something or are you dudes hating on the coalesce operator?
|
#
?
May 22, 2011 07:53
|
|
- Molog
- Mar 4, 2004
-
Title text
|
Wheany posted:
SubObject sub = thisObject.getSubObject();
OtherSubObject other = sub.getOtherSubObject();
other.performAction();
or
thisObject.
getSubObject().
getOtherSubObject().
performAction();
If only there was a third better option.
|
#
?
May 22, 2011 10:35
|
|
- schnarf
- Jun 1, 2002
-
I WIN.
|
All hail my terrible C++ template metaprogramming FizzBuzz: http://codepad.org/MCDF6Zt9. There's no runtime branching.
I'd really like to concatenate the entire string at compile time, but that seems hard.
schnarf fucked around with this message at 16:48 on May 22, 2011
|
#
?
May 22, 2011 16:42
|
|
- Zombywuf
- Mar 29, 2008
-
|
schnarf posted:
I'd really like to concatenate the entire string at compile time, but that seems hard.
Consider something like:
code:template<class T, class List>
struct list_to_string {
static const T value = List::head::value;
list_to_string<T, List::tail>;
};
template<class T>
struct list_to_string<T, Null> {
static const T value = 0;
};
list_to_string<char, fizzbuzz::type> fizzbuzz_;
char *fizzbuzz_string = reinterpret_cast<char *>(&fizzbuzz_);
|
#
?
May 22, 2011 17:11
|
|
- Milotic
- Mar 4, 2009
-
9CL apologist
-
Slippery Tilde
|
Look at those C/C++ guys frontin' with their pointers and templates. Time to step to and show them how we do this stuff in the .NET world.
code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FBConsole
{
public static class CustomExtensions
{
public static IEnumerable<TSource> AllOrDefaultIfEmpty<TSource>(this IEnumerable<TSource> source, TSource defaultValue)
{
if (source.Count() != 0)
{
foreach (var tSourceValue in source)
{
yield return tSourceValue;
}
}
else
{
yield return defaultValue;
}
}
public delegate void Func<TSource>(TSource source);
public static IEnumerable<TSource> ForEach<TSource>(this IEnumerable<TSource> source, Func<TSource> func)
{
foreach (var tSourceValue in source)
{
func(tSourceValue);
yield return tSourceValue;
}
}
}
public class FizzBuzzCharacter
{
public string CharacterOrNumber { get; set; }
public int Order { get; set; }
public bool DivisibleByThree { get; set; }
public bool DivisibleByFive { get; set; }
public bool DivisibleByFifteen { get; set; }
public bool DivisibleByNada { get; set; }
}
static class Program
{
static void Main(string[] args)
{
List<FizzBuzzCharacter> fbCharacters = new List<FizzBuzzCharacter>();
//Populate the list.
fbCharacters.AddRange("fizz".ToCharArray()
.Select((c, index) => new FizzBuzzCharacter() {
CharacterOrNumber = c.ToString(),
Order = index,
DivisibleByThree = true,
DivisibleByFive = false,
DivisibleByFifteen = true,
DivisibleByNada = false
}));
fbCharacters.AddRange("buzz".ToCharArray()
.Select((c, index) => new FizzBuzzCharacter() {
CharacterOrNumber = c.ToString(),
Order = index + 4,
DivisibleByThree = false,
DivisibleByFive = true,
DivisibleByFifteen = true,
DivisibleByNada = false
}));
//Don't forget the new line character for matches!
fbCharacters.AddRange("\n".ToCharArray()
.Select((c, index) => new FizzBuzzCharacter() {
CharacterOrNumber = c.ToString(),
Order = 9,
DivisibleByThree = true,
DivisibleByFive = true,
DivisibleByFifteen = true,
DivisibleByNada = false
}));
//Select the Fizz Buzz characters which match
for (int i = 1; i <= 100; i++)
{
Type fbType = typeof(FizzBuzzCharacter);
string methodName = string.Format("DivisibleBy{0}", i % 15 == 0 ? "Fifteen" : i % 5 == 0 ? "Five" : i % 3 == 0 ? "Three" : "Nada");
var defaultFbc = new FizzBuzzCharacter() { CharacterOrNumber = i.ToString() + "\n" };
var results = fbCharacters
.Where(fbc => (bool)fbType.InvokeMember(methodName, System.Reflection.BindingFlags.GetProperty, null, fbc, null))
.OrderBy(fbc => fbc.Order)
.AllOrDefaultIfEmpty(defaultFbc)
.ForEach(fbc => Console.Write(fbc.CharacterOrNumber));
int x = results.Count(); //Force evaluation
}
}
}
}
|
#
?
May 22, 2011 18:26
|
|
- New Yorp New Yorp
- Jul 18, 2003
-
Only in Kenya.
-
Pillbug
|
Milotic posted:
Look at those C/C++ guys frontin' with their pointers and templates. Time to step to and show them how we do this stuff in the .NET world.
That's beautifully horrible. Kudos.
|
#
?
May 23, 2011 04:48
|
|
- Smugdog Millionaire
- Sep 14, 2002
-
8) Blame Icefrog
|
As long as we're talking horrible C# fizzbuzz implementations, we have include melonhead's reflection version: http://dotnetpad.net/ViewPaste/SrmP90hoE02rYAyTIVHc3w
|
#
?
May 23, 2011 06:52
|
|
- Scaevolus
- Apr 16, 2007
-
|
code:#include <stdio.h>
int main(int n) {
printf("%d\n\0 Fizz\n\0Buzz\n\0FizzBuzz\n"+(!(n%3)+2*!(n%5))*6,n)&&n%100&&main(n+1);
}
|
#
?
May 23, 2011 10:23
|
|
- Milotic
- Mar 4, 2009
-
9CL apologist
-
Slippery Tilde
|
Thank you for all the kind words. I found it surprisingly fun. There's a couple of problems with that solution, one of which being it is far too quick - you don't really get the excitement of waiting for the solution. We could throw in Thread.Sleeps, but that's far too predictable. We want people on the edge of their seats.
So I present: GeneticFizzBuzz, with a mutation chance of around 1 in 1 million.
code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GeneticFizzBuzzConsole
{
class Program
{
static void Main(string[] args)
{
DateTime startTime = DateTime.Now;
var idealFizzBuzz = FizzBuzzSelector.BreedTheBuggersUntilOneFits();
DateTime endTime = DateTime.Now;
foreach (KeyValuePair<int, string> kvp in idealFizzBuzz.Map)
{
Console.WriteLine(string.Format("{0} : {1}", kvp.Key, kvp.Value));
}
}
}
public class FizzBuzzSelector
{
public static FizzBuzzCandidateSolution BreedTheBuggersUntilOneFits()
{
//Mitochondrial Eve
var eve = new FizzBuzzCandidateSolution();
for (int i = 1; i <= 100; i++) { eve.Map.Add(i, "Fizz"); }
//Piltdown Buzz
var piltdown = new FizzBuzzCandidateSolution();
for (int i = 1; i <= 100; i++) { piltdown.Map.Add(i, "Buzz"); }
var population = new List<FizzBuzzCandidateSolution>();
population.Add(eve);
population.Add(piltdown);
double generations = 0;
int bestMatch = 0;
while (!population.Any(p => p.Suitability() == 100))
{
//Select the top two from the population
var topFittest = population.OrderByDescending(p => p.Suitability()).Take(2);
var newMother = topFittest.ElementAt(0);
var newFather = topFittest.ElementAt(1);
var baby = FizzBuzzBreeder.Mate(newMother, newFather);
population = new List<FizzBuzzCandidateSolution>();
population.Add(newMother); population.Add(newFather); population.Add(baby);
generations++;
int candidateBestMatch = Math.Max(
Math.Max(newMother.Suitability(), newFather.Suitability())
, baby.Suitability());
if (candidateBestMatch > bestMatch)
{
bestMatch = candidateBestMatch;
Console.WriteLine(string.Format("New best Match at {0} generations: {1}", generations, bestMatch));
}
}
return population.First(p => p.Suitability() == 100);
}
}
public class FizzBuzzCandidateSolution
{
public FizzBuzzCandidateSolution()
{
Map = new Dictionary<int, string>();
}
public Dictionary<int, string> Map { get; set; }
private int? _Score = null;
//Assume we only call this once and the Map never changes contents
public int Suitability()
{
if (_Score == null)
{
_Score = 0;
foreach (KeyValuePair<int, string> kvp in Map)
{
if (Suitable(kvp.Key, kvp.Value)) { _Score++; }
}
}
return _Score.Value;
}
public static bool Suitable(int i, string value)
{
if (i % 15 == 0 && value.Equals("FizzBuzz")) { return true; }
else if ((i % 15 != 0) && (i % 5 == 0) && value.Equals("Buzz")) { return true; }
else if ((i % 15 != 0) && (i % 3 == 0) && value.Equals("Fizz")) { return true; }
else if ((i % 5 != 0) && (i % 3 != 0) && value.Equals(i.ToString())) { return true; }
return false;
}
}
public class FizzBuzzBreeder
{
private static Random Chance = new Random();
private static Random PerformMutationChance = new Random();
public static FizzBuzzCandidateSolution Mate(FizzBuzzCandidateSolution mother, FizzBuzzCandidateSolution father)
{
var child = new FizzBuzzCandidateSolution();
foreach (var key in mother.Map.Keys)
{
var childValue = MateValue(key, mother.Map[key], father.Map[key]);
child.Map.Add(key, childValue);
}
return child;
}
private static string MateValue(int key, string motherValue, string fatherValue)
{
//Can we just use the mother or father?
if (FizzBuzzCandidateSolution.Suitable(key, motherValue))
{
return motherValue;
}
if (FizzBuzzCandidateSolution.Suitable(key, fatherValue))
{
return fatherValue;
}
//Should we use the mutation?
int performMutation = PerformMutationChance.Next(1000000);
if (performMutation == 0)
{
//Not suitable, let's randomly mutate.
int mutationChance = Chance.Next(4);
//Work out what the mutation would be:
string mutation;
switch (mutationChance)
{
case 0: mutation = key.ToString(); break;
case 1: mutation = "Fizz"; break;
case 2: mutation = "Buzz"; break;
case 3: mutation = "FizzBuzz"; break;
default: mutation = "IMPOSSIBLE"; break;
}
return mutation;
}
else if (performMutation % 2 == 0)
{
return motherValue;
}
else
{
return fatherValue;
}
}
}
}
It took me about 26 minutes 47 seconds to compute the results on my PC and required 25880962 generations. But next time it could be as much as an hour, as little as 1 second.
Here's how it progressed
(Why yes, I am on holiday)
|
#
?
May 23, 2011 14:29
|
|
- ninjeff
- Jan 19, 2004
-
|
Milotic posted:code:public static bool Suitable(int i, string value)
{
if (i % 15 == 0 && value.Equals("FizzBuzz")) { return true; }
else if ((i % 15 != 0) && (i % 5 == 0) && value.Equals("Buzz")) { return true; }
else if ((i % 15 != 0) && (i % 3 == 0) && value.Equals("Fizz")) { return true; }
else if ((i % 5 != 0) && (i % 3 != 0) && value.Equals(i.ToString())) { return true; }
return false;
}
This is the best part by far.
|
#
?
May 23, 2011 14:46
|
|
- The Saddest Robot
- Apr 17, 2007
-
|
This is disgusting and almost made me vomit up my coffee.
I think that I may need consoling now.
|
#
?
May 23, 2011 18:04
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
?
Apr 28, 2024 14:51
|
|
- qntm
- Jun 17, 2009
-
|
code:print ["FizzBuzz", $_, $_, "Fizz", $_, "Buzz", "Fizz", $_, $_, "Fizz", "Buzz", $_, "Fizz", $_, $_ ]->[$_ % 15]."\n" for 0..99
I don't think I'm doing it right
|
#
?
May 24, 2011 00:07
|
|