public string Render()
{
if ((format & TextFormat.Bold) == TextFormat.Bold)
{
AddTag("[b]");
}
if ((format & TextFormat.Italic) == TextFormat.Italic)
{
AddTag("[i]");
}
if ((format & TextFormat.Underlined) == TextFormat.Underlined)
{
AddTag("[u]");
}
if ((format & TextFormat.Overlined) == TextFormat.Overlined)
{
AddTag("[o]");
}
return str;
}
}
public interface ITextFormattable
{
void ApplyFormat(Text text);
}
public class B : ITextFormattable
{
public void ApplyFormat(Text text)
{
text.format |= TextFormat.Bold;
}
}
public class I : ITextFormattable
{
public void ApplyFormat(Text text)
{
text.format |= TextFormat.Italic;
}
}
public class U : ITextFormattable
{
public void ApplyFormat(Text text)
{
text.format |= TextFormat.Underlined;
}
}
public class O : ITextFormattable
{
public void ApplyFormat(Text text)
{
text.format |= TextFormat.Overlined;
}
}
public class TextFormatterFactory
{
public ITextFormattable CreateTextFormatterFromString(string tag)
{
switch (tag)
{
case "b":
return new B();
case "i":
return new I();
case "u":
return new U();
case "o":
return new O();
default:
throw new ArgumentException("tag");
}
}
}
public static class Program
{
public static void Main(string[] args)
{
Text text = new Text("ENTERPRISE");
TextFormatterFactory tff = new TextFormatterFactory();
B boldFormatter = (B)tff.CreateTextFormatterFromString("b");
boldFormatter.ApplyFormat(text);
I italicFormatter = (I)tff.CreateTextFormatterFromString("i");
italicFormatter.ApplyFormat(text);
U underlineFormatter = (U)tff.CreateTextFormatterFromString("u");
underlineFormatter.ApplyFormat(text);
O overlineFormatter = (O)tff.CreateTextFormatterFromString("o");;
overlineFormatter.ApplyFormat(text);
Console.Write(text.Render());
}
}
}
Name:
Anonymous2012-09-04 6:39
>>9
Javascript is amazing. You should have also added some node.js in it to ensure proper parallelization in the cloud.
Name:
Anonymous2012-09-04 21:35
FFOC:
local mt
mt = {
__call = function (self, text)
return self.starttags .. text .. self.endtags
end,
__index = function (self, key)
return setmetatable({ starttags = ("%s[%s]"):format(self.starttags, key), endtags = ("[/%s]%s"):format(key, self.endtags) }, mt)
end
}
fun bbcode tag str = "["^tag^"]"^str^"[/"^tag^"]"
val bold = bbcode "b"
val italic = bbcode "i"
val overline = bbcode "o"
val underline = bbcode "u"
val () = (print o bold o italic o overline o underline) "ENTERPRISE QUALITY"
Name:
Anonymous2012-09-04 22:07
The Achilles Heel of JavaScript is its inconsistent behavior across implementations. It is virtually impossible to use it to do anything robust on the client side.
JavaScript is weakly typed, and the automatic coercions that it does can produce surprising results. "2" + "3" is "23" (+, when applied to two strings, performs concatenation); "2" * "3" is 6. (Multiplication not defined for strings; so the language tries converting them to numerals, succeeds, and multiplies those). This can be surprising.
Rather annoying gotcha in array constructor. new Array() produces empty array; new Array(2,3) produces array with 2 and 3 as its elements. new Array(5) does not produce array with 5 as its single element; instead, it returns a 5-element array!
Whereas most languages have one universal singular value (null/nil/Void/whatever); JavaScript has three - "false", "null" and "undefined". That leads to confusing and irregular semantics.
No integral types - the only numeric type supported is an IEEE754 double-precision float. For a scripting language, this limitation is probably less obnoxious than it would be elsewhere.
Language has LexicalScoping, but with an interesting twist. Unlike JavaScripts brethen C/C++/Java/C#/etc, where variables introduced in an anonymous block within a function are only valid within that block; in JavaScript such variables are valid for the entire function.
Every script is executed in a single global namespace that is accessible in browsers with the window object.
Automatic type conversion between strings and numbers, combined with '+' overloaded to mean concatenation and addition. This creates very counterintuitive effects if you accidentally convert a number to a string:
var i = 1;
// some code
i = i + ""; // oops!
// some more code
i + 1; // evaluates to the String '11'
i - 1; // evaluates to the Number 0
The automatic type conversion of the + function also leads to the intuitive effect that += 1 is different then the ++ operator:
var j = "1";
j++; // j becomes 2
>>15
The core behaviour of JavaScript is quite consistent across implementations, as it's specified in great detail by the ECMA-262 standard. The host objects (for example, the DOM in browsers) do have differences, but these are less problematic than they used to be. You can generally follow W3 specs relating to HTML and DOM in any modern browser and get consistent results.
No integral types - the only numeric type supported is an IEEE754 double-precision float. For a scripting language, this limitation is probably less obnoxious than it would be elsewhere. http://www.khronos.org/registry/typedarray/specs/latest/
Name:
Anonymous2012-09-04 22:59
>>17
so now it has the overhead of double->whatever and whatever->double casts all the time