Defining a monad in Haskell

Question: I am currently reading Kan Extensions for Program Optimisation and in the first page of the paper the author defines given M a monad the following monad
type C a = ∀z . (a → M z ) → M z
instance Monad C where
  return a = λc → c a
  m >>= k = λc → m (λa → k a c)

So I've tried to play a bit with it using M=Maybe hence writing the following code
type C a = forall z . (a -> Maybe z) -> Maybe z

instance Monad C where
  return a = \c -> c a
  m >>= k = \c -> m $ \a -> k a c

But it doesn't compile and outputs

Main.hs:22:10: error:
• The type synonym 'C' should have 1 argument, but has been given none

Which I am unable to interpret. What is wrong here ?
(I've done some category theory but I've just started learning Haskell).
Comment: In Haskell type synonyms cannot be partially applied, for complicated technical reasons. Make it a `newtype` instead.
Comment: Well I guess you made a typo on your syntax then
Comment: @Julia You can't just replace the keyword `type` with `newtype`. `newtype` is more similar to `data` than it is to `type`; you need to define its constructor on the right hand side, not just write a type expression.
Comment: @Julia Research papers often give their examples in pseudo-code rather than actual code, exactly so they don't have to worry about things like the difference between `type` and `newtype`, and can just write down ideas. This example is pseudo-Haskell (the lambdas are a dead giveaway; `UnicodeSyntax` can give you the fancy arrows, but not `λ`). If you want to compile something similar you need to translate it into actual Haskell first. May I humbly suggest that "I've just started learning Haskell" is not the right experience level to be translating ideas from research papers into working code?
Comment: @FyodorSoikin I've already (naively) tried doing so before but I got an other error. "Cannot parse data constructor in a data/newtype declaration"
Comment: @FyodorSoikin I just replaced `type` with `newtype`. newtype C a = forall z . (a -> Maybe z) -> Maybe z
Comment: FYI this is an example of what is called the Continuation monad, which if you google you'll find lots of good references for. It takes a bit of getting your head around what it's doing and what it's useful for! (Your example specialises the `r` in `Cont r a` to be an instantiated type constructor `M z`, but there's no need for that in general - I presume there is in the wider context of the source you're quoting the definition from.)
Comment: @FyodorSoikin: The deeper reasons might be complicated, but there's a fairly simple explanation. Partially applied type synonyms would let us make type-level closures. In turn, that would cause unreliable type inference (and worse error messages) when using higher-kinded types such as `Functor` and `Monad`.
Comment: It exists in a library [`Codensity`](https://hackage.haskell.org/package/kan-extensions-5.2.5/docs/Control-Monad-Codensity.html)
Answer: As noted in the comments, you need a newtype. type aliases are just programmer conveniences and aren't distinct entities, and you want to implement a typeclass, so you need a distinct entity. You can write the newtype like so.
newtype C a = C (forall z . (a -> Maybe z) -> Maybe z)

Though I would recommend including an accessor, because we'll need it for marshaling in a moment.
newtype C a = C { unC :: forall z . (a -> Maybe z) -> Maybe z }

(If you're following along at home, you need {-# LANGUAGE RankNTypes #-} to be able to do this forall syntax. It's a compiler extension that enables that particular fancier type)
To be clear, we're not going to write a Monad instance for forall z. (a -> Maybe z) -> Maybe z. That type doesn't belong to us. We're going to write a Monad instance for C a, a new type we just made up which happens to be isomorphic to forall z. (a -> Maybe z) -> Maybe z in a nice way.
Now Monad depends on Functor and Applicative, so we'll start there.
Functor is derive-able, so we could just write deriving (Functor), but it's also good practice to know how to write these out.
instance Functor C where
    fmap f (C a) = C $ \k -> a (k . f)

Now Applicative. It's very common to implement Applicative in terms of a corresponding Monad instance, so we'll do just that here.
instance Applicative C where
    pure = return
    (<*>) = ap

(Note: ap comes from the Control.Monad module, so you'll want to import that as well)
This always works, provided you write the Monad instance so as not to call pure or <*>.
Now the Monad. It's pretty much exactly what you wrote, except with some C and unC marshaling to make the type checker happy.
instance Monad C where
    return a = C (\c -> c a)
    m >>= k = C (\c -> unC m $ \a -> unC (k a) c)

Finally, note that we never actually used Maybe or any of its properties here. Not only do we not care what that M is, we don't even need it to be a Functor. So we can parameterize by that as well.
{-# LANGUAGE RankNTypes #-}

import Control.Monad

newtype C m a = C { unC :: forall z . (a -> m z) -> m z }

instance Functor (C m) where
    fmap f (C a) = C $ \k -> a (k . f)

instance Applicative (C m) where
    pure = return
    (<*>) = ap

instance Monad (C m) where
    return a = C (\c -> c a)
    m >>= k = C (\c -> unC m $ \a -> unC (k a) c)

Now C Maybe is identical to the C type you were just working with, but we can put anything of kind * -> * in place of the m. Anything, not just Functors. Go wild.
Answer: You can not use type aliases that are partially applied for instances. What is usually done in that case is working with a newtype that creates a new type, but is in essence only the type it wraps:
{-# LANGUAGE RankNTypes #-}
{-# Language UnicodeSyntax #-}

newtype C m a = C { getC :: ∀z . (a → m z ) → m z}

and then define the instance as:
instance Monad m => Monad (C m) where
  return a = C (\c -> c a)
  C m >>= k = C (\c -> m (\a -> getC (k a) c))

it thus does some wrapping and unwrapping in the C constructor, but these are essentially no-ops.

Defining a monad in Haskell


Telerik MVC grid issue in insert mode without edit mode

Question: HI
I have telerik mvc grid in ajax mode and editing is enabled. In my scenario i have to use only the insert mode so I disabled the edit mode. but when I run i get the javascript error on click of "Add New Record". Sample project can be downloaded from Here
Answer: I got this to work by the following:
function GridNotes_onEdit(e) {
    $('#Content').after("")
        .after("");
}

Now, this worked when I only had one field being inserted.  If you have multiple columns, you may need to customize it to your specifications.  But it is do-able.  As well, it worked in a detail view grid.
Hope this helps...
Comment: I had to modify the code a bit to get it working in my situation. I used `$('.t-grid-new-row td:nth-child(6)').html('< class...` in the OnEdit function, where the 6 (1 based) was the column I wanted to put my buttons.
Answer: If you are looking for an official support channel as you provided a sample project, post your request to the telerik MVC forums on their site.
Comment: the link is from there post but got no response yet :(
Answer: Had the same issue. The answer is in-line needs the edit buttons to be there, so with in-line mode the insert breaks
This link covers this and has a fix.
http://www.telerik.com/community/forums/aspnet-mvc/grid/insert-only-with-server-editing.aspx


How can I remove the word ".rs" from a string using VimScript?

Question: How can I remove the word ".rs" from a string using VimScript?
:! "rustc" a:arq
:! "./" a:arq
however this second paragraph is not the compiled file, and it causes an error while performing my function. I wanted to remove this ".rs" so that it will execute the file after compilation (which by default is the same name as the compiled file)
Answer: :! "./" . substitute(a:arq, '\.rs$', '', '')

Replace '.rs' at the end of the a:arq with an empty string.
See http://vimdoc.sourceforge.net/htmldoc/eval.html#substitute()


How to get number of list element (ul tag) of HTML using Get matching XPath count?

Question: I'm kind of new to XPATH-query. I use RF and selenium2library and the XPath Helper-plugin in chrome to test a certain website page. I'm new to HTML/CSS/JavaScript as well.
The web page consists of two ULs (lists) for left and right sides of the page and each one has a few LIs which have few divisions comprised of widgets (JPEG images etc).
I need to count this list rows (number of LIs in each UL). I have already done the samething  in a drop down menu to count its elements with no problem (perhaps because it was considered
a web element). But right now I use the same "Get Matching Xpath Count" which returns almost the whole page HTML source instead of a number and it then fails.
All my program is based on getting the number of LIs in a UL (of drop down menu, page, table,...). so I wonder what to do now. Here is an example of the HTML code of the page:

and my code was:
Get Matching Xpath Count  |  //ul[@id="ccol8a580456553ae"]/li

which does give me some texts plus HTML code.i also tried:
Get Length |  //ul[@id="ccol8a580456553ae"]

which doesn't give me 2 but a big number.
Answer: An XPath 2.0 expression to count the 'li' for the specific '' would be:
//ul[@id="col8a580456553ae"]/count(li)

Answer: Try this new chrome extension
https://chrome.google.com/webstore/detail/relative-xpath-helper/eanaofphbanknlngejejepmfomkjaiic
Answer: You've  made a typo in the id value - an extra "c" char in the beginning; otherwise the xpath is correct:
${count}=    Get Matching Xpath Count    //ul[@id="col8a580456553ae"]/li

By the way, the keyword Get Matching Xpath Count is deprecated in the latest version of the SeleniumLibrary, in favour of Get Element Count


Using scala macro to manipulate variable declaration

Question: i would like to use scala (v2.12.8) macros to manipulate all variable declarations of an given block. In this example to add the value 23.
For example:
val myblock = mymanipulator {
    var x = 1
    x = 4
    var y = 1
    x + y
  }
print( myblock )

becomes
{
  var x = (1).+(23);
  x = 4;
  var y = (1).+(23);
  x.+(y)
}

For this, I implemented mymanipulator like this:
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.language.implicitConversions

object mymanipulator {
  def apply[T](x: => T): T = macro impl

  def impl(c: Context)(x: c.Tree) = { import c.universe._

    val q"..$stats" = x
    val loggedStats = stats.flatMap { stat =>

      stat match {
        case ValDef(mods, sym, tpt, rhs) => {
          List( q"var $sym : $tpt = $rhs + 23" )
        }

        case _ => {
          List( stat )
        }
      }

    }

    val combined = q"..$loggedStats"

    c.info(c.enclosingPosition, "combined: " + showRaw(combined), true)

    combined
  }
}

And I get this information during compilation of the macro:
Information:(21, 31) combined: {
  var x = (1).+(23);
  x = 4;
  var y = (1).+(23);
  x.+(y)
}
  val myblock = mymanipulator {

But when I execute the mymanipulator with the given block above, then I get this error message:
Error:scalac: Error while emitting Test.scala
variable y

This error occurs also when I change the implementation to do nothing:
 stat match {
        case ValDef(mods, sym, tpt, rhs) => {
          List( q"var $sym : $tpt = $rhs" )
        }

        case _ => {
          List( stat )
        }
      }

Only when I return stat the error vanishes
 stat match {
    case ValDef(mods, sym, tpt, rhs) => {
       List( stat )
     }

     case _ => {
        List( stat )
      }
  }

Can somebody tell me that I am doing wrong? Thanks
Answer: You should untypecheck the tree before transformations
  object mymanipulator {
    def apply[T](x: => T): T = macro impl

    def impl(c: blackbox.Context)(x: c.Tree): c.Tree = {
      import c.universe._

      val q"..$stats" = c.untypecheck(x) // here
      val loggedStats = stats.flatMap { stat =>

        stat match {
          case ValDef(mods, sym, tpt, rhs) /*q"$mods var $sym : $tpt = $rhs"*/ => {
            List( q"$mods var $sym : $tpt = $rhs + 23" )
          }

          case _ => {
            List( stat )
          }
        }

      }

      val combined = q"..$loggedStats"

      c.info(c.enclosingPosition, "combined: " + showRaw(combined), true)

      combined
    }
  }

Macro untypecheck required
What is wrong with this def macro?
Answer: Untypechecking the source tree drops the types of subtrees.
This makes impossible to do tree manipulating based on types of expressions.
So how to work with typechecked tree and replace the terms definitions in the macro source code?
If we replace the definition of term (or even simply reassemble the same term definition, actually having new tree) then compiler fails on Ident of that term with error like:
Could not find proxy for val <val-name>

REPL error is like
Error while emitting <console>
variable j

Simple untypechecking or even further typechechking of the resulting tree does not helps.
There are several reasons I've found in different answers:

Old (reused from source tree) Ident still refers to it's old Symbol definition that is already is absent in resulting tree (we replaced it)
The reused unchanged Symbol definition (its tree) has changed its owner (the code was wrapped or rewrapped)

The solution that helps me is to recreate all Idents which refers to local (to source code) definitions. Idents that refer to outer Symbols definitions should stay unchanged (like 'scala', internally referred outer types, etc.) otherwise compiling fails.
The following sample (runnable in IDEA Worksheet in REPL mode, tried in 2.12) shows usage of Transformer to recreate Idents that refer to local definition only. New replaced Ident will not now refer to old definition.
It uses syntax tree that covers the only required Scala syntax to reach the goal. Everything unknow by this syntax becomes OtherTree that holds subtree of original source code.
import scala.reflect.macros.blackbox
import scala.language.experimental.macros

trait SyntaxTree {
  val c: blackbox.Context
  import c.universe._

  sealed trait Expression

  case class SimpleVal(termName: TermName, expression: Expression) extends Expression
  case class StatementsBlock(tpe: Type, statements: Seq[Expression], expression: Expression) extends Expression
  case class OtherTree(tpe: Type, tree: Tree) extends Expression

  object Expression {
    implicit val expressionUnliftable: Unliftable[Expression] = Unliftable[Expression] (({
      case q"val ${termName: TermName} = ${expression: Expression}" =>
        SimpleVal(termName, expression)
      case tree@Block(_, _) => // matching on block quosiquotes directly produces StackOverflow in this Syntax: on the single OtherTree node
        val q"{ ..${statements: Seq[Expression]}; ${expression: Expression} }" = tree
        StatementsBlock(tree.tpe, statements, expression)
      case otherTree =>
        OtherTree(otherTree.tpe, otherTree)
    }: PartialFunction[Tree, Expression]).andThen(e => {println("Unlifted", e); e}))

    implicit val expressionLiftable: Liftable[Expression] = Liftable[Expression] {
      case SimpleVal(termName, expression) =>
        q"val $termName = $expression + 23"
      case StatementsBlock(_, statements, expression) =>
        q"{ ..${statements: Seq[Expression]}; ${expression: Expression} }"
      case OtherTree(_, otherTree) =>
        c.untypecheck(otherTree) // untypecheck here or before final emitting of the resulting Tree: fun, but in my complex syntax tree this dilemma has 0,01% tests impact (in both cases different complex tests fails in ToolBox)
    }
  }
}

class ValMacro(val c: blackbox.Context) extends SyntaxTree {
  import c.universe._

  def valMacroImpl(doTransform: c.Expr[Boolean], doInitialUntypecheck: c.Expr[Boolean])(inputCode: c.Expr[Any]): c.Tree = {
    val shouldDoTransform = doTransform.tree.toString == "true"
    val shouldUntypecheckInput = doInitialUntypecheck.tree.toString == "true"

    val inputTree = if (shouldUntypecheckInput)
      c.untypecheck(inputCode.tree) // initial untypecheck helps but we loose parsed expression types for analyses
    else
      inputCode.tree

    val outputTree: Tree = inputTree match {
      case q"${inputExpression: Expression}" =>
        val liftedTree = q"$inputExpression"
        if (shouldDoTransform) {
          val transformer = new LocalIdentsTransformer(inputTree)
          transformer.transform(liftedTree)
        } else
          liftedTree
      case _ =>
        q"{ ${"unexpected input tree"} }"
    }
    println(s"Output tree: $outputTree")
    /*c.typecheck(c.untypecheck(*/outputTree/*))*/ // nothing commented helps without transforming (recreating) Idents
  }

  class LocalIdentsTransformer(initialTree: Tree) extends Transformer {
    // transform is mandatory in any case to relink (here: reset) Ident's owners when working with typechecked trees
    private val localDefSymbols: Set[Symbol] = initialTree.collect {
      case t if t != null && t.isDef && t.symbol.isTerm =>
        t.symbol
    }.toSet

    println("localDefSymbols", localDefSymbols)

    override def transform(tree: Tree): Tree = tree match {
      case tree@Ident(termName: TermName) if localDefSymbols.contains(tree.symbol) =>
        println("replacing local Ident", termName, tree.symbol)
        Ident(termName)
      case _ =>
        super.transform(tree)
    }
  }
}

def valMacro(doTransform: Boolean, doInitialUntypecheck: Boolean)(inputCode: Any): Any = macro ValMacro.valMacroImpl

val outerVal = 5
// 1) works with pre untypechecking, but we loose types info
valMacro(false, true) {
  val i = 1
  i + outerVal
}
// 2) works with Transformer
valMacro(true, false) {
  val i = 1
  i + outerVal
}
// 3) does not work
valMacro(false, false) {
  val i = 1
  i + outerVal
}
// 4) cases when we reuse old tree without changes: fails
valMacro(false, false) {
  var j = 1
  j
}
// 5) cases when we reuse old tree without changes: works
valMacro(true, false) {
  var j = 1
  j
}

Output:
// 1) works with pre untypechecking, but we loose types info
(Unlifted,OtherTree(null,1))
(Unlifted,SimpleVal(i,OtherTree(null,1)))
(Unlifted,OtherTree(null,i.+($line8.$read.INSTANCE.$iw.$iw.outerVal)))
(Unlifted,StatementsBlock(null,List(SimpleVal(i,OtherTree(null,1))),OtherTree(null,i.+($line8.$read.INSTANCE.$iw.$iw.outerVal))))
Output tree: {
  val i = 1.$plus(23);
  i.+($line8.$read.INSTANCE.$iw.$iw.outerVal)
}
res0: Any = 29

// 2) works with Transformer
Unlifted,OtherTree(Int(1),1))
(Unlifted,SimpleVal(i,OtherTree(Int(1),1)))
(Unlifted,OtherTree(Int,i.+($line8.$read.INSTANCE.$iw.$iw.outerVal)))
(Unlifted,StatementsBlock(Int,List(SimpleVal(i,OtherTree(Int(1),1))),OtherTree(Int,i.+($line8.$read.INSTANCE.$iw.$iw.outerVal))))
(localDefSymbols,Set(value i))
(replacing local Ident,i,value i)
Output tree: {
  val i = 1.$plus(23);
  i.+($line8.$read.INSTANCE.$iw.$iw.outerVal)
}
res1: Any = 29

// 3) does not work
(Unlifted,OtherTree(Int(1),1))
(Unlifted,SimpleVal(i,OtherTree(Int(1),1)))
(Unlifted,OtherTree(Int,i.+($line8.$read.INSTANCE.$iw.$iw.outerVal)))
(Unlifted,StatementsBlock(Int,List(SimpleVal(i,OtherTree(Int(1),1))),OtherTree(Int,i.+($line8.$read.INSTANCE.$iw.$iw.outerVal))))
Output tree: {
  val i = 1.$plus(23);
  i.+($line8.$read.INSTANCE.$iw.$iw.outerVal)
}

Error while emitting <console>
value i

// 4) case when we reuse old tree without changes: fails
(Unlifted,OtherTree(<notype>,var j: Int = 1))
(Unlifted,OtherTree(Int,j))
(Unlifted,StatementsBlock(Int,List(OtherTree(<notype>,var j: Int = 1)),OtherTree(Int,j)))
Output tree: {
  var j = 1;
  j
}

Error while emitting <console>
variable j

// 5) case when we reuse old tree without changes: works with Transformer
(Unlifted,OtherTree(<notype>,var j: Int = 1))
(Unlifted,OtherTree(Int,j))
(Unlifted,StatementsBlock(Int,List(OtherTree(<notype>,var j: Int = 1)),OtherTree(Int,j)))
(localDefSymbols,Set(variable j))
(replacing local Ident,j,variable j)
Output tree: {
  var j = 1;
  j
}

If to skip untypchecking of OtherTree (while lifting it) or do not do untypchecking of theresulting tree then we'll get the error in 2) sample macro call:
java.lang.AssertionError: assertion failed:
  transformCaseApply: name = i tree = i / class scala.reflect.internal.Trees$Ident
     while compiling: <console>
        during phase: refchecks
     library version: version 2.12.12
    compiler version: version 2.12.12

Actually this sample show 2 different approaches to work with typechecked input tree:

How to use simple AST that covers only the part of Scala syntax tree with required types collection (it is actually may not be used). If such AST is not used and resulting tree is built "on the fly" using parts of the source typechecked tree (resulting partially typechecked tree) then resulting tree should be untypechecked (after transformation) before emitting
How to fix Idents in partially typechecked resulting tree with LocalIdentsTransformer


Stuck allen bolt - out of ideas

Question: I could use some help on how to move forward to remove couple of bolts. I am currently out of ideas.
To give some context, I noticed that my scooter windshield was not exactly leveled and I thought to remove it and set it up again properly.
I had no problem removing the bottom two hex screws, but the ones above were stuck. So I bought one of these extractors you can see below:

The problem is - I am unable to drill into the screw. It seems to be just too hard to penetrate.
I have attached some photos here, where you can also notice that the left screw one has some plastic stuff coming out, that makes me think somebody put some glue in the screw back in the time.
I am open to any suggestion to get this out. For the record, these are M6 x 30mm bolts.
Thank you!
Comment: Get better drill bits. In particular, don't get ones listed only for wood. A nice new metal drill bit should go through steel like a knife through butter (well, cool butter, not melted butter).
Comment: start with a very small drill bit and work your way up
Comment: Why does the second photo appear to show that you _were_ successful in drilling?
Comment: I have tried different drill sizes and they all seem to be able to scratch the surface of the bolt, but at a certain point it sounds like they stop being effective and get stuck
Comment: Scooter repair is not home improvement. There is a site for motor vehicles.
Comment: Your drill bit marks indicate that your drill size has been way too big. For M6 bolts you should be using something like an 2mm or 3mm drill bit. With everything that small, there's a good chance that you'll bust off your extractor off (if not your drill bit). Maybe you should look into "heli-coils." Or take this job to a professional shop, as the tooling for installing heli-coils is probably more expensive than a shop's labor.
Comment: If its been glued, then you may have difficulty removing it!!
Comment: I'm voting to close this question because its not about home improvement.
Comment: The last pic, with the actual close up of the screw heads - I hope the top screw didn't look like the bottom screw when you started. That looks like a perfect hex head for an Allen key. IMHO, you may well have ruined a perfectly good bolt!
Answer: A cobalt drill bit will work. A carbide bit would work faster, but the cobalt bit will be more forgiving against breakage.
Noting the 10.9 stamped on the bolt head, ASTM F568 specs the hardness of 10.9 grade bolts as ranging from 33 to 39 on the Rockwell C scale. For comparison, a cobalt drill bit has a hardness of about C65. A carbide bit has a hardness of about C75.
Comment: I have not tried cobalt drill bit - but the ones I tried (De Vault) are allegedly for metal as well. I will give it a try and see if that works!
Answer: Keep using larger drill bits until you've removed the entire screw head from the center out. Once the head is gone and you can get the windscreen off, you should be able to clamp a pair of vise grips on the shaft of the screw.
Or, maybe you could grind or file a slot across the head of the screw to accept a flat screwdriver blade? That approach might require sacrificing the chrome trim piece.

Stuck allen bolt - out of ideas


How create a settings menu with checkbox Android

Question: I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
Answer: <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
     <PreferenceCategory
           android:summary="@string/summary_category"
           android:title="@string/title_category">
           <CheckBoxPreference
                 android:key="main"
                 android:defaultValue="true"
                 android:summary="@string/summary_main"
                 android:title="@string/title_main"
          />
          <CheckBoxPreference
                android:key="firstDependent"
                android:summary="@string/summary_firstDependent"
                android:title="@string/title_firstDependent"
                android:dependancy="main"
          />
          <CheckBoxPreference
                android:key="secondDependent"
                android:summary="@string/summary_secondDependent"
                android:title="@string/title_secondDependent"
                android:dependancy="main"
          />
    </PreferenceCategory>

</PreferenceScreen>

You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that.
Then do the following.
public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

    }

}

You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.

Hope this helps.
Comment: Thanks. Have i to set something in the java?
Answer: You have to do it like in that example, but you'll have three checkboxes instead of one. If you want two checkboxes to be disabled until the first one is true you can use the android:dependency property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
    android:summary="..."
    android:title="..." >

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="first"
        android:summary="@string/summary_first"
        android:title="@string/title_first" />

    <CheckBoxPreference
        android:defaultValue="false"
        android:dependency="first"
        android:key="second"
        android:summary="@string/summary_second"
        android:title="@string/title_second" />

    <CheckBoxPreference
        android:defaultValue="false"
        android:dependency="first"
        android:key="third"
        android:summary="@string/summary_third"
        android:title="@string/title_third" />
</PreferenceCategory>


Convergence of the $a_1 = a, a_{n + 1} = \frac{2}{a_n + 1}$ sequences by using subsequences

Question: I supposed to find a candidate limit and show the monotonicity of the $a_{2n}$ and $a_{2n + 1}$ subsequences of the given sequence:
$$a_1 = a$$
$$a_{n + 1} = \frac{2}{a_n + 1}$$
$$with \quad0 < a < 1$$
Finding a candidate limit is easy just supposing that $a_{n} \to L$ and with a uniqueness of the limit I can solve $a_{n+1} \to L$ and it gives me L = 1.
But for proving that the subsequences $a_{2n}$ and $a_{2n + 1}$ are monotone decreasing and increasing respectively and converge to the same limit I need to find some type of recurrence relation between the members of that sequence. The best I found is:
$$a_{2(n+1)} = \frac{2a_{2n} + 2}{a_{2n} + 3}$$
$$a_{2(n + 1)+1} = \frac{2a_{2n + 1} + 2}{a_{2n + 1} + 3}$$
But this doesn't give me any good recurrence relation between the subsequence members. Any help?
Thank you
Comment: study $f(f(x))-x$ for $f(x)=2/(1+x)$.
Comment: Hint for monotonicity: $\displaystyle\;a_{n + 1} - a_n= \frac{2}{a_n + 1}-\frac{2}{a_{n-1} + 1}=\frac{-2(a_n-a_{n-1})}{(a_n + 1)(a_{n-1} + 1)}\,$, so the difference between consecutive terms changes sign at each step.
Answer: .Since $a_1=a>0$ the recurrence relation implies that $a_n>0$ for all n. Note that $$a_{n+1}(a_n+1)=2$$ for all positive integers n.
Therefore $$  a_{n+1}(a_n+1)=a_{n+3}(a_{n+2}+1)=2$$ The above equality implies that  if $$a_{n+1}<a_{n+3}$$ then
$$a_{n}>a_{n+2}.$$ That is, if one of your subsequences is increasing the other is decreasing.
Note that the function $$f(x)=\frac {2}{x+1}$$ has a fixed point at $ x=1 $ with  $f'(1)=-1/2.$ Thus $ x=1$ is an attractor. The interval$(0,1)$ is contained in the basin of attraction therefore starting at x=a, every subsequence of iterates converges to $ L=1.$
Comment: Hmmm ... "that if ..." doesn't prove the existence of 2 stable subsequences. It  doesn't prove that both subsequences are bounded and thus have limits. And doesn't prove that both limits are equal. Still a lot to do ...
Answer: It's easy to show that $a_n >0, \forall n$. Then let's look at the sequence $b_n=|a_n-1|$ then we have
$$0\leq b_{n+1}=|a_{n+1}-1|=\left|\frac{2}{a_n+1}-1\right|=\left|\frac{a_n-1}{a_n+1}\right|=\frac{b_n}{a_n+1}\leq b_n \tag{1}$$
$(b_n)_{n\geq1}$ is descending and lower bounded by $0$ so it's converging. If we assume that $\lim\limits_{n\rightarrow\infty}b_n=L>0$ then we have $\forall \varepsilon >0$
$$0<L<b_n<L+\varepsilon \tag{2}$$
for all $n$ from some $N(\varepsilon)$ onwards. But, some of the $a_n
\geq1$, it's easy to show that $0<a_n\leq 1 \iff 1<a_n+1\leq 2 \iff \frac{1}{2} < \frac{a_n+1}{2}\leq 1\iff 2 > a_{n+1} \geq 1$. By induction we can show that $0<a_{2k+1}\leq 1$ and $1\leq a_{2k} < 2$, since $0<a_1< 1$.
So, from the definition of the limit, we will have $n=2k > N(\varepsilon)$ and $n+1=2k+1 > N(\varepsilon)$ with $b_n$ and $b_{n+1}$ satisfying $(1)$ and $(2)$ while:
$$0<L<b_{2k+1}=\frac{b_{2k}}{a_{2k}+1}\leq \frac{b_{2k}}{2}<\frac{L+\varepsilon}{2}$$
which is clearly a contradiction for $0<\varepsilon<L$. So $(2)$ doesn't holf for $\forall \varepsilon >0$ and as a result
$$\lim\limits_{n\rightarrow\infty}b_n=0 \Rightarrow \lim\limits_{n\rightarrow\infty}a_n=1$$


Determining the basis of a kernel

Question: Let $U = P_3(\Bbb R)$, the vector space of polynomials of degree at most $3$ in a formal variable $t$.
Let $f : U → \Bbb R$ be the linear map defined by $f (u) = u′(1)$, where $u′(1)$ is the derivative of $u$ (with respect to $t$) evaluated at $t = 1$
(i) Determine the matrix of f with respect to the bases {$1$,$t$,$t^2,t^3$} and {1}.
(ii) Determine the rank and the nullity of $f$ , and find a basis of the kernel of $f$ .
My attempt
(i)
$$ \left[
    \begin{matrix}{}
      0&1&2&3\\
    \end{matrix}
\right] $$
(ii) As it is already in reduced row echelon form we can proceed,
Would I be correct in saying that this matrix has rank 1 (because it has 1 leading 1) and therefore by the rank and nullity theorem it has nullity 3?
Therefore we have the equation $x_2+2x_3+3x_4= 0$, therefore we can set the value of any three variables and work out the last one. So the basis I got was:
Let $x_1=1,x_2=1,x_3=1$ then we have $x_4= -1$
Let $x_1=1,x_2=0,x_3=0$ then we have $x_4= 0$
Let $x_1=0,x_2=0,x_3=1$ then we have $x_4= -\frac{2}{3}$
Therefore a basis for the kernel is {$(1+t+t^2-t^3),(1),(t^2-\frac{2}{3}t^3)$}
Is this correct?
Comment: Is this correct?
Comment: $f(t^2-\frac23)\ne0$
Comment: Sorry I'll correct that mistake
Comment: Is it correct now?
Answer: Looks like you've found a basis for the kernel, but there's a more systematic way to go about this once you have the rref matrix. Find the columns that don't have pivots (leading entries). Those will be the free coordinates in the kernel basis. Set each one to one and the rest to zero in turn, and solve for the remaining coordinates. You'll find very quickly that "solving" for the missing values amounts to reading them from the column in the rref matrix that corresponds to the coordinate that's been set to $1$, then negating them.
In this case, the vectors constructed in this way will be $(1,x_2,0,0)^T$, $(0,x_2,1,0)^T$ and $(0,x_2,0,1)^T$. The missing coordinates are, respectively, $0$, $-2$ and $-3$, i.e., the negations of the entries in the first, third and last columns.
Comment: Thank you so much for this, super helpful!
Answer: Here is another approach which uses the polynomial structure. First, let s=t-1. Then every polynomial can be written as $u=a_0+a_1 s + a_2 s^2 + a_3 s^3$ and now the map is $f(u) = a_1$. Thus, the kernel is spanned by $1,s^2,s^3$ since these polynomials and their combinations have $a_1=0$. Thus, $1,(t-1)^2,(t-1)^3$ is a basis!

Determining the basis of a kernel


Conduit Underground - Ways for tunneling under obstructions?

Question: I'm running into a situation where it's increasingly difficult to bury this PVC underground 18" in some spots due to massive tree roots. Changing directions and/or destroying the roots isn't an option. The isolated roots are easy enough to tunnel under but there are root 'clusters' that are close together are the hard part. I did bore a hole in those roots about 9" deep and wanted to use rigid metal conduit, however the size I need to use (1.25") makes it difficult to source (rent, not purchase) a conduit bender capable of bending this size.
I read in the NEC table 300.5 that PVC is under 2" of 'concrete or equivalent' than it can be as close as 12" from grade. My question is: What is 'equivalent'?
Also, I tried the 'pressure water' method for tunneling, it got real messy real fast and my pipe didn't stay level, probably doing it wrong.
Looking at a few tools such as Borit as a next resort (something cheaper is ideal b/c I'll probably only use it once and I never quite get around to reselling a tool), and calling someone to finish this short portion of trench as a final resort. If anyone can offer insight as to either finishing this trench at 18", or having the PVC above that with 2" concrete (or equivalent?)
Also, one idea I just had is running the PVC and forming concrete outside the trench, and dropping it in the trench after it's hardened, hopefully that makes sense.
Update
Tomorrow (Weather contingent) I will try shop vac'ing around the roots, OP's yard in that video appears to have some root clusters and it looks like he did an excellent job getting the dirt from around them. Patience I have, money I don't.
The difficult can be done right away, the impossible takes a little longer.
Comment: Have you considered an overhead feeder?
Comment: A worthy challenge for sure.  Is there an alternate route that would work better for trenching or boring?  Like along the edge of the property, even if it is four times longer than going through the trees.
Comment: nope, it's in the backyard, and the tree covers most of it, so no matter the direction from house to garage I'm pretty certain I'll run into the same problem :(
Comment: No, but I wouldn't be opposed to the idea, just would have liked to *not* have dug this trench first though, of course ;)
Comment: String up an overhead feeder, and turn the trench in to a water feature (a babbling brook might be relaxing).
Comment: @Tester101 I'd be willing to u/v a detailed answer about doing that, for reference the garage is about 30 ft away from the house, and I'm looking to power a 100amp subpanel.
Answer: 300.5 also specs 4" of concrete allows 0" cover (other than the 4" concrete) if it's wide enough (6" sideways from the conduit each way.) I believe that "equivalent" would allow things like concrete pavers or bricks as well as poured-in-place concrete for in-trench conduit cover.
Not sure how you approached "pressure water" boring - I'd use a section of the conduit size you want to run, or larger, with a section of smaller pipe inside it to run the water in - waste-water and dirt comes out the space between the two pipes, and the larger outer pipe directs the "drilling" - but roots and stones will stop that method, and it's not good for corners.
For tools you only need once, remember rental. But sometimes rental costs as much as having someone come do it.
I can't imagine how you'd envision pouring concrete separate from the trench to be in any way easier or more effective than pouring it in the trench. I can only see that as a complication that makes everything more difficult. Backfill and compact level with the top of the conduit, shape the trench to be wide enough for how deep the conduit is buried, pour the concrete - nothing easier than that if you have to use concrete cover.


Gufw doesn't show my current iptables rules

Question: I bought a new machine, so, what I did was save my current iptables using iptables-save and imported the rules into the new machine with iptables-restore command.
If I run iptables -L command in the new machine I can see all my rules, but if I open Gufw I can't see them.
Somebody can help me?
nonox
Answer: Gufw is a front-end for ufw, which in turn is a simple interface/front-end to iptables.
Gufw does not display your existing iptables rules because it is only designed to interpret the high-level ufw rules; it simply cannot parse the low-level iptables rules.
Comment: Thanks!
You right, what I did was save my current ufw rules (/lib/ufw/user.rules) and copy them in my new machine and everything works fine!


How can I insulate a room without tearing down walls?

Question: The kitchen in my Philadelphia row home seems to have been a last minute idea. After last winter, I'm almost certain there is no insulation installed in the room. Is there a way for me to do something about this?
I can't break down the walls or do anything like replace them. I'm only renting, and my landlord isn't ever around or helpful so it's DIY without a lot of leeway.
But I would also love to not have to have a coat to get a glass of water, if anyone has any ideas? Cold air even comes through little holes in the wall-where it looks like they put in a mug drying rack, changed their mind, and that was that.
Comment: You likely have a some kind of balloon framing in the kitchen, where the stud cavity in the kitchen goes unobstructed to the unheated attic crawl space. Do you have a drop ceiling or any way to reach the studs at the ceiling of the kitchen, or at the ceiling of the second floor bath (if you have one)?
Comment: You should be able to find a local company, that can inject insulation into the walls though small holes cut through the drywall.  Once you patch the holes, you'll never be able to tell they were there (except that you'll be warmer). They'll have to cut one hole per stud bay, so it may be a lot of patching though.
Comment: Note Amanda - that the foam injections are far from perfect and the results can vary from pretty good to useless.
Comment: Those holes in the wall should be your first priority. Patching them should not only make the place look nicer, but prevent air from escaping. Check for any other drafts you can find while you're at it.
Answer: Foam insulation can be injected into the stud cavities. A contractor will drill one hole in each stud cavity to inject the foam through. Then you can patch the holes and paint over them.
You should also check any windows in the room. Look specifically at the caulking around the frame. If it's old and dry/cracked, remove the old caulk, use window/door expanding foam to fill large gaps, and apply fresh caulk.
You can also purchase window insulation kits that use double sided tape and secure a plastic sheet all the way around the window frame. Then a hair dryer/heat gun is used to shrink the plastic sheet in place. The trapped air provides a bit of insulation, and the plastic stops drafts.
If there are any exterior doors in the room, check the weatherstripping for wear and cracks. Consider replacing/upgrading the weatherstripping to stop drafts.
Comment: Blown-in fiber insulation is also a possibility.


dripping water from the shower area

Question: Just moved into our new rented apartment and having trouble keeping the water from dripping out the bath tub area.
the side of the shower curtain that is closer to the shower head is okay as we have secured it with a shower curtain clip. The side opposite has a ledge that extends beyond the end of the tub. The water that drips and splashes from the side flows down to the opposite side of the tub and onto the ledge and then drips on to the floor. Unfortunately, The shower curtain is not long enough to stretch this far out to be clipped on the other end too. The only way I feel I can prevent water from flowing down and eventually dripping to the floor is if we created a slant (incline) to prevent the water from flowing down like it does now. But as mentioned before, we live in a rented apartment and can not make any structural changes to the bathroom. Any advise would be much appreciated. Thank you very much in advance!
Comment: +1 for the great diagram.  From the shape of your puddle the floor looks pretty flat.
Comment: short term solution: put a cloth on the side before the ledge and let a corner hang down in the tub
Answer: Perhaps you can run a line of sealant from the wall to the edge of the bath, creating a route for the water to take into the bath. Sealant is easily removed, so this wouldn't count as a structural modification in my eyes.
This should do the job, provided your bath is flush with the ledge. Here's a little diagram in keeping with your own:

dripping water from the shower area


When is it appropriate to use sample_weights in keras?

Question: According to this question, I learnt that class_weight in keras is applying a weighted loss during training, and sample_weight is doing something sample-wise if I don't have equal confidence in all the training samples.
So my questions would be,

Is the loss during validation weighted by the class_weight, or is it only weighted during training?
My dataset has 2 classes, and I don't actually have a seriously imbalanced class ditribution. The ratio is approx. 1.7 : 1. Is that neccessary to use class_weight to balance the loss or even use oversampling? Is that OK to leave the slightly imbalanced data as the usual dataset treated?
Can I simply consider sample_weight as the weights I give to each train sample?  And my trainig samples can be treated with equal confidence, so I probably I don't need to use this.
Answer:
From the keras documentation it says

class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

So class_weight does only affect the loss during traning. I myself have been interested in understanding how the class and sample weights is handled during testing and training. Looking at the keras github repo and the code for metric and loss, it does not seem that either loss or metric is affected by them. The printed values are quite hard to track in the training code like model.fit() and its corresponding tensorflow backend training functions. So I decided to make a test code to test the possible scenarios, see code below. The conclusion is that both class_weight and sample_weight only affect training loss, no effect on any metrics or validation loss. A little surprising as val_sample_weights (which you can specify) seems to do nothing(??).

This types of question always depends on you problem, how skewed the date is and in what way you try to optimize the model. Are you optimizing for accuracy, then as long as the training data is equally skewed as when the model is in production, the best result will be achieved just training without any over/under sampling and/or class weights.
If you on the other hand have something where one class is more important (or expensive) than another then you should be weighting the data. For example in fraud prevention, where fraud normally is much more expensive than the income of non-fraud. I would suggest you try out unweighted classes, weighted classes and some under/over-sampling and check which gives the best validation results. Use a validation function (or write your own) that best will compare different models (for-example weighting true-positive, false-positive, true-negative and false-negative differently dependent on cost).
A relatively new loss-function that has shown great result at kaggle competitions on skewed data is Focal-loss. Focal-loss reduce the need for over/under-sampling. Unfortunately Focal-loss is not a built inn function in keras (yet), but can be manually programmed.
Yes I think you are correct. I normally use sample_weight for two reasons. 1, the training data have some kind of measuring uncertainty, which if known can be used to weight accurate data more than inaccurate measurements. Or 2, we can weight newer data more than old, forcing the model do adapt to new behavior more quickly, without ignoring valuable old data.

The code for comparing with and without class_weights and sample_weights, while holding the model and everything else static.
import tensorflow as tf
import numpy as np

data_size = 100
input_size=3
classes=3

x_train = np.random.rand(data_size ,input_size)
y_train= np.random.randint(0,classes,data_size )
#sample_weight_train = np.random.rand(data_size)
x_val = np.random.rand(data_size ,input_size)
y_val= np.random.randint(0,classes,data_size )
#sample_weight_val = np.random.rand(data_size )

inputs = tf.keras.layers.Input(shape=(input_size))
pred=tf.keras.layers.Dense(classes, activation='softmax')(inputs)

model = tf.keras.models.Model(inputs=inputs, outputs=pred)

loss = tf.keras.losses.sparse_categorical_crossentropy
metrics = tf.keras.metrics.sparse_categorical_accuracy

model.compile(loss=loss , metrics=[metrics], optimizer='adam')

# Make model static, so we can compare it between different scenarios
for layer in model.layers:
    layer.trainable = False

# base model no weights (same result as without class_weights)
# model.fit(x=x_train,y=y_train, validation_data=(x_val,y_val))
class_weights={0:1.,1:1.,2:1.}
model.fit(x=x_train,y=y_train, class_weight=class_weights, validation_data=(x_val,y_val))
# which outputs:
> loss: 1.1882 - sparse_categorical_accuracy: 0.3300 - val_loss: 1.1965 - val_sparse_categorical_accuracy: 0.3100

#changing the class weights to zero, to check which loss and metric that is affected
class_weights={0:0,1:0,2:0}
model.fit(x=x_train,y=y_train, class_weight=class_weights, validation_data=(x_val,y_val))
# which outputs:
> loss: 0.0000e+00 - sparse_categorical_accuracy: 0.3300 - val_loss: 1.1945 - val_sparse_categorical_accuracy: 0.3100

#changing the sample_weights to zero, to check which loss and metric that is affected
sample_weight_train = np.zeros(100)
sample_weight_val = np.zeros(100)
model.fit(x=x_train,y=y_train,sample_weight=sample_weight_train, validation_data=(x_val,y_val,sample_weight_val))
# which outputs:
> loss: 0.0000e+00 - sparse_categorical_accuracy: 0.3300 - val_loss: 1.1931 - val_sparse_categorical_accuracy: 0.3100

There are some small deviations between using weights and not (even when all weights are one), possible due to fit using different backend functions for weighted and unweighted data or due to rounding error?
Comment: For reference, if you specify weighted_metrics instead of metrics then they do get passed on. See @kawingkelvin's answer in the link above for an example
Comment: Thanks for your detailed answer. I have further question about the question 2, which I find it difficult for me to understand, but I will post an another question for that. BTW, would you recommend some materials on the validation functions that evaluates different models` performance? Anything like github repos or kaggle kernels would be appreciated.
Comment: I am using sample_weight and found out it doesn't actually get passed into computing metrics during training (.fit). https://stackoverflow.com/questions/64380057/tf-2-3-0-training-keras-model-using-tf-dataset-with-sample-weights-does-not-appl


OO Style - Simple question

Question: Simple question for all you pragmatic object-oriented fellas.
I have read many times to avoid classes like "Processor", and "xxxxHandler" in order to agree to OO standards: and I believe it's a good measure for understandability of the system code.
Let's assume we have a software that scans some file structure, let's say a bunch of specific CSV files. Let's say we have an independent module called CsvParser.
class CsvParser {
    public string GetToken(int position) { .. }
    public bool ReadLine() { .. }
}

class MyCsvFile {
    public string FullPath { get; }

    public void Scan() {
        CsvParser csvp(FullPath);
        while (csvp.ReadLine())
        {
            /* Parse the file that this class represents */
        }
    }
}

This will save having a "FileScanner" class, which is a -Processor- type class. Something that will collect say, a bunch of files from a directory, and scan each.
class MyFileScan {
    public string[] Files { get; set; }

    public void GetFiles() { this.Files = Directory.GetFiles(..); }

    public void ScanFiles() {
        foreach (string thisFilePath in Files)
        {
           CsvParser csvp(thisFilePath);
           /* ... */
        }
    }
}

The OO approach dictates having the MyCsvFile class, and then a method representing the operation on the object.
Any thoughts? What do you programmers think.
Comment: Shouldn't this me community wiki?  (I can't believe I've become one of the people who ask this question.)
Answer: I'd agree with your philospohy but if it was me I'd probably call the class CsvFile and have a Parse method in addition to the Scan one. In OO programming it's always desireable to make your classes represent "things" (nouns in English).
That aside if I was asked to maintain your code I'd grasp what a CsvParser class is likely to be doing whereas MyFileScan would send me into fits of rage and cause me to have to read the code to work it out.
Comment: Also, naming things "MyX" drives me crazy.  Its fine if you're giving a very general explanatory example such as MyObject or MyClass (although in that case I would go with the metasyntactic variables: foo, bar, baz, qux, quux, corge, grault, garply, waldo, fred, plugh, xyzzy, thud).  But if you're going to be using a class for anything, I want to see CSV not MyCSV.  To whom does this CSV belong and why are they claiming ownership?
Comment: Indeed correct and I totally agree. I'm using 'My' for example purposes here. I wanted to keep to my domain here without using foo, bar, etc. Good point.
Answer: This is Problem Domain vs. Solution Domain design.
In order to solve a problem, we can design our class to model real life objects, that is program according to Problem Domain.
Another way of programming is design according to Solution Domain.
For instance, when we are designing a Flight booking system, for Flight management expert, they will describe the flight trip as "route", "time", "angle" (I cann't really recall the term). If we design according to these model, it is called design according to Problem Domain.
We can also design using coordinate system (x, y, z), because we feel that as a programmer, we can deal with these more efficiently. This is design for Solution Domain.
The problem with Solution domain is, in the world of project, one thing which is constant is - CHANGE! the requirements will always change! If the requirements are change, you have to redesign you program.
However, If you model you classes as real life object, you are less affected by the changes, because real-life objects seldom change.
"Processor", and "xxxxHandler" <-- this is design to solution domain.
You could take a look at Domain-Driven Design --- DDD for shorts.
Comment: I see, I see.. will definitely look into it - "wikipediaing" as I speak. You see I think this has a lot to do with thought process.. how we model things. You're saying basically that how we use the objects is more likely to change than the object themselves? Or am I misreading.
Thanks
Comment: I am saying, the requirements from customer always change. If we design our classes based on real life model, it will immune from the change of requirement.
Answer: I think what you're describing is that objects should take care of operations that only require themselves, which is in general a good rule to follow.  There's nothing wrong with a "processor" class, as long as it "processes" a few different (but related) things.  But if you have a class that only processes one thing (like a CSV parser only parses CSVs) then really there's no reason for the thing that the processor processes not to do the processing on itself.
However, there is a common reason for breaking this rule: usually you don't want to do things you don't have to do.  For example, with your CSV class, if all you want is to find the row in the CSV where the first cell is "Bob" and get the third column in that row (which is, say, Bob's birth date) then you don't want to read in the entire file, parse it, and then search through the nice data structure you just created: it's inefficient, especially if your CSV has 100K lines and Bob's entry was on line 5.
You could redesign your CSV class to do small-scale operations on CSV's, like skipping to the next line and getting the first cell.  But now you're implementing methods that you wouldn't really speak of a CSV having.  CSV's don't read lines, they store them.  They don't find cells, they just have them.  Furthermore, if you want to do a large-scale operation such as reading in the entire CSV and sorting the lines by the first cell, you'll wish you had your old way of reading in the entire file, parsing it, and going over the whole data structure you created.  You could do both in the same class, but now your class is really two classes for two different purposes.  Your class has lost cohesion and any instance of the class you create is going to have twice as much baggage, while you're only likely to use half of it.
In this case, it makes sense to have a high-level abstraction of the CSV (for the large-scale operations) and a "processor" class for low-level operations. (The following is written in Java since I know that better than I know C#):
public class CSV
{
    final private String filename;
    private String[][] data;
    private boolean loaded;

    public CSV(String filename) { ... }

    public boolean isLoaded() { ... }
    public void load() { ... }
    public void saveChanges() { ... }
    public void insertRowAt(int rowIndex, String[] row) { ... }
    public void sortRowsByColumn(int columnIndex) { ... }

    ...
}

public class CSVReader
{
    /*
     * This kind of thing is reasonably implemented as a subclassable singleton
     * because it doesn't hold state but you might want to subclass it, perhaps with
     * a processor class for another tabular file format.
     */
    protected CSVReader();
    protected static class SingletonHolder
    {
        final public static CSVReader instance = new CSVReader();
    }

    public static CSVReader getInstance()
    {
        return SingletonHolder.instance;
    }

    public String getCell(String filename, int row, int column) { ... }
    public String searchRelative(String filename,
        String searchValue,
        int searchColumn,
        int returnColumn)
    { ... }

    ...
}

A similar well-known example of this is SAX and DOM.  SAX is the low-level, fine-grained access, while DOM is the high-level abstraction.
Comment: Had to pick this as it has opened my eyes a bit: I've bin wondering a bit on how I could abstract the CSVParser. At this point of time, it will only have read-only operations on the file.. but eventually yes requirements change. I'll take sipwiz's advice and let the object do what it needs. Thanks guys.

OO Style - Simple question


Python and Selenium: Automatically adjusting range depending on if something exists

Question: The first post on Stackoverflow, this website has been very useful to me in the past so wanted to thank the community first and foremost. I've been learning Python over the last 2-3 weeks, just by doing my own little "projects", and I had a question that I tried searching for but was not really sure on how to phrase it so finding an answer is a bit difficult.
Essentially what I would like to do is take a user input for a Pokémon, go to a website which has information on it and prints a table of the moves that Pokémon can learn by leveling up. I've managed to get a code running, however, the issue I have is that each Pokémon learns a different number of moves. The code I have is:
import selenium.webdriver as webdriver

def moves(x):
        move = browser.find_element_by_xpath("""//*[@id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]/td[2]/a""").text
        return(move)

poke = input("Search for which Pokémon?: ")
browser = webdriver.PhantomJS()
browser.get("https://pokemondb.net/pokedex/" + str(poke))
for x in range(1,50):
    print(moves(x))

If a Pokémon only has 15 moves it learns by level up, then on the 16th iteration of x, an error is returned because that xpath doesn't exist, so I am looking for a way to modify my code such that it stops printing if the xpath doesn't exist.
I was thinking using a while True statement, but not too sure how to approach it. Again, I'm very new to Python so the code may not be the most elegant.
Thanks for reading!
Answer: easiest option here is to add try-except to pass error without breaking loop:
...
(your code above)
while 1: #initially was for x in range(1,50), but while loop is better
    try:
        print(moves(x))
    except:
        break

Answer: Using while loop with a try except statement. So if the element is not present it just stops the loop.
def moves(x):
    while True:
        try:
            move = browser.find_element_by_xpath("""//*[@id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]/td[2]/a""").text
        except:
            break
        return(move)

Comment: Thank you, this helped me on my way! I still ended up with a bunch of "None" falling out of the valid range, but added an if statement to solve this :)
Comment: Happy to help :)
Answer: If the difference between each move is in the
 tag you can locate list of all those elements and use it to get the data you are looking for
def moves(element):
    move = element.find_element_by_xpath('//*td[2]/a').text
    return(move)

browser.get("https://pokemondb.net/pokedex/" + str(poke))
moves_list = browser.find_element_by_xpath("""//*[@id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]""")
for x in range(1, len(moves_list)):
    print(moves(moves_list[x]))


Water tank burner goes out after moving temperature dial from lowest settings to "Low" indicator

Question: My water tank is a:

I've already replaced the thermocouple, but the problem occurs after I get the pilot light started and release the red push button

I move the upper dial to the on position from the pilot position.
Then I start turning the temperature dial clockwise. The burner lights, then quickly goes out.
This video shows the process from the point of letting go of the red push button:
https://imgur.com/R7v6tLw
Does anyone have any thoughts on this?
Answer: Did you follow the lighting instructions precisely, particularly this part of step 8:
"... Continue to hold the 'red button' in for about one (1) minute after the pilot is lit.  Release button and it will pop back up.  Pilot should remain lit.  If it goes out, repeat steps 2 through 8."
I would make sure to do this, and test to see if the pilot light stays lit without adjusting the temperature yet.
I'm also unsure if your temperature dial is too far to the low side - it's gone beyond the little index marks on the knob.  In the manual, the lowest mark is the lowest temperature setting, so not sure why your knob goes past there.  edit: now looking at the manual and comparing your knob, your knob has less index marks, so perhaps this is fine.
The manual also says (page 18):

The gas control (thermostat) is constructed with a built in safety shut-off device designed to shut off the gas supply to the burner if the pilot flame is extinguished for any reason.

The gas control (thermostat) is also equipped with a single use gas shut off device that will shut off the gas supply to the burner if the water heater exceeds normal operating temperatures.

In your video, when you turn the gas on, it looks like it's coming on pretty rapidly - this could be due to turning the knob too fast or something funny with the markings like I mentioned above.  This excess gas pressure could be blowing out the pilot light and causing the thermostat to shut off the gas supply to the burner.


Convert One 3-Way Light Switch to Belkin WeMo Light Switch (Single Pole)

Question: Our foyer chandelier has three switches that control it. I want to control it with a Belkin WeMo light switch, which specifically states is not compatible with 3-way switches/fixtures, sadly. Well, if I can't convert one of the three switches to be a Belkin WeMo light switch and keep the other two switches working, I'd like to just convert one of the switches to be a WeMo light switch as a single pole, such that the other two switches would simply be left in their on positions.
In my case, the light switch I want to convert/replace has four wires. Two black wires. Two white wires. There appears to be a couple loops of bare copper wire inside the metal box, but currently it does not appear to be connected to the existing switch in any way.
The new Belkin WeMo light switch has four wires of its own. Two black (labeled switch, one with a lightbulb icon, the other with a lightning bolt icon), one white (labeled neutral, with a zero icon), and one green (labeled ground, with the standard icon for ground).

So how might I go about figuring out what wires on the WeMo light switch to connect to what wires currently connected to the existing 3-way switch to pull off this functionality?
Any tricks/hacks that might help me keep the functionality of at least one of the two other three-way switches and have the third WeMo light switch altogether? I do wish Belkin would just come out with a 3-way version already, but until that happens, I guess I'm stuck taking this route if I want the WeMo capabilities (and I do).
Comment: It sounds like the switch you are looking at is the 4way that sits in the middle of the chain.  If it is wired with romex there should be 2 red wires as well.  Typically, you have source (lightning bolt), 3 way switch, 4 way switch, 3 way switch and then light in a chain.  It is also possible to wire it with source and light and then switches but that middle 4 way switch will always have 6 wires with insulation on it.  Two of the wires will just be wire capped together, 4 connected to switch.  Check to see what is happening in the other two switches.
Comment: How, precisely, is the old switch wired? There is a way to do what you ask, using a device known as a [relay](http://www.grainger.com/product/FUNCTIONAL-DEVICES-INC-RIB-Enclosed-Pre-Wired-Relay-5LE35?s_pp=false&picUrl=//static.grainger.com/rp/s/is/image/Grainger/5LE35_AS01?$smthumb$#reviews) to interface between the "on/off" of your WeMo and the "either/or" or "changeover" functions of your 3 and 4 way switches, respectively.  (A relay is simply a switch that is turned on and off by an electromagnet, instead of by someone flipping it. :)  However, I'd need the existing wiring for a full answer
Comment: For future visitors, WeMo has _finally_ released a 3-way version of their light switch.  No, there still is not a 3-way dimmer, but Lutron does have them.
Answer: SHORT ANSWER
You can't convert the WeMo switch to replace a three way. You can add it to a three way circuit to turn lights on and off if and only if the three ways were first set to leave the light on. When you turn off the WeMo (at home or remotely), the lights will go off. When you turn on the WeMo (at home or remotely) the lights will go on. But, if the lights are already off because the three ways were used to turn them off, the WeMo will not turn them on.
LONG STORY
The WeMo light switch is a simple on and off switch (as far as your electrical system is concerned; yes, I know it does more). It allows a circuit to be on or off, based on whether that switch is on or off, period.
A three way switch also turns a circuit on or off. The difference is a three way switch is one of a pair. Each switch changes the state that the other switch has created. For the switch closest to the power supply (A), current comes into the switch on one side (on the common terminal) and leaves the switch on one of the two terminals on the other side (the travelers), depending on which direction the switch is thrown.
On the second three way switch (B), closer to the light or other device, the power comes in on one of the two travelers and leaves the switch via the common if and only if the switch is thrown to the same traveler as switch A.
For three way switches, there is no on or off position. Rather there is traveler 1 and traveler 2 positions (not labeled that way but called that for illustration). The circuit is on when both switches are turned to the same traveler and off when they are turned to different travelers.

<code>This illustration also shows the 4-way switch described by OP.
It also does NOT show neutral and ground wires which MUST be connected as well.

All of the illustrations show just one ON position for the 3-way/4-way setup.
There are actually four possible ON combinations and four OFF combination.
</code>
The WeMo can be added to this circuit by placing it between the hot line and the common terminal on three way switch A or between the common terminal and the fixture on switch B.

or

<code> These illustrations do NOT show neutral and ground wires which MUST be connected as well.
</code>
WeMo effectively interrupts or connects the power either before or after it goes through the three way setup. However, If it three way pair is off (the swithces are not turned to the same traveler), then the circuit will stay of, regardless of the WeMo setting.
Whether this works for you depends on whether you are willing to be sure to turn the three way setup to on before you leave and then control the system with the WeMo.
THE SETUP
The existing switch you are describing does not sound like a three way, but a four way. This is a switch used between two three ways to give an additional location to switch a circuit on and off. (The white wires are actually being used as hot lines and should have been marked with black tape or a black marker). This switch cannot be controlled by the WeMo. You need to find the one of the real three ways to make this work.
When you do, connect the wires as follows:

WeMo GREEN wire to the BARE wires in the box.
WeMo WHITE to WHITE wires in the box.

If you are placing the WeMo next to three way A (closer to line voltage):

WeMo BLACK (Lightning bolt) to BLACK or RED power line currently attached to COMMON terminal on existing three way switch.
WeMo BLACK (Light bulb) to COMMON terminal of existing three way switch.

If you are placing the WeMo next to three way B (closer to fixture):

WeMo BLACK (Lightning bolt) to COMMON terminal of existing three way switch.
WeMo BLACK (Light bulb) to BLACK or RED power line currently attached to COMMON terminal on existing three way switch.

REMEMBER, the three way setup has to be set so that the circuit is ON for the WeMo to be able to control it remotely. (The four way you have been looking at complicates the circuitry, but you can effectively ignore it; just make sure the overall circuit is ON before you leave; it doesn't matter which switch you flip to turn it on.)
SUPPLEMENT
Based on the OP's comment, he or she is willing to lose the 3/4-way functionality and install the WeMo only to control the fixture. That cannot be done by replacing the 4-way switch described because there is no neutral in that box.
The WeMo could be swapped for one of the three ways if there is a neutral in the three way box.
If you are replacing 3-way A, attach the black lightning wire to the black (or red) hot from the line. Attach the black lamp wire from the switch to the black traveler. Cap the white traveler. In the 4-way box, remove the switch and join the two black traveler wires. Cap the whites separately. Place a blank cover plate over the box. In switch box B (closest to fixture) remove the switch. Attach the black traveler wire to the fixture wire. Cap the white traveler wire.
If you are replacing 3-way B, follow the same steps, except in box A, the black traveler is attached to the hot line, and in Box B the black lightning wire is attached to the black traveler and the black lamp wire is attached to the fixture wire.
Comment: Removing the switches and splicing power straight through would be another option.
Comment: @Tester101 Absolutely. I got the impression OP was trying to preserve the 3/4-way options for use when on site.
Comment: Yes, I'm aware I would lose the 3/4-way functionality by doing this, I was just thinking wishfully. Since there is no trick/hack, then no, I don't intend to use the other switches once I do convert one of them to the WeMo. With Tester101's suggested approach would I be able to put the WeMo switch at the location I mentioned, which you said was currently a 4-way? If so, how might I wire it up after removing the 4-way switch?
Comment: What, relays never were invented in your world?
Answer: There is one way to turn it wemo into a 3/4 way switch.
For this example I will use a 3way switch( which is my setup)
I have one switch set as a "master"
)
At this point you have installed the 1st wemo light switch just as per the instructions.
For the 2ed light switch(or 3rd)
These are "slaves"
So the ground wire (green) to the bare copper wire
The white wire is connected to the neutral wire (should be white, but it may require switching around some of the wires in each box, more on that later)
The 1st black wire, goes to the feed (the black wire supplying the power)
And the second back wire is unused. Cap it off.
You may have to make changes to where each wire goes in each electrical box.
I had to.
At this point you need to setup an account with ifttt ( if this then that )
And for a 3 way switch create 2 recipes.
1: when your 2ed "slave" switch is turned on (the if)
Then toggle the master switch (the that)
2: when your 2ed "slave" switch is turned on (the if)
Then turn the 2ed "slave" switch off (the that)
For a 4 way switch
You would need 4 recipes (two per switch)
The only draw back at this point is that there is a delay when using ifttt, about 30 seconds or so.
I'm trying to find someway not to use ifttt, there seem be to some people working on some projects that might be able to use a local server instead.
Answer: Well technically you could just add another switch (being the WeMo) with a new hot leg tied directly to the chandelier.  And keep you 3/4 way setup.  With only one drawback.  The WeMo will be god and the 3/4 way will function as normal when the WeMo is off (A good setup if you intend to use WeMo mostly for away mode or alarm mode).  You tie the power and neutral in straight from mains line in.  And connect the WeMo out (light-bulb) to the chandelier (or the 3 way b common).  The WeMo can turn the lights on and off.  When the WeMo is on the lights will always be on.  When the WeMo is off the lights will be on manual control. Either on or off depending on the last setting.
Comment: I thought you guys had easy questions on here.  This is harder than Elec. Engineering.


Consume web service with DJANGO

Question: I have a django application to show some data from my database (ORACLE), but now I need to show some data from a web service.
I need to build a form based in the request of the web service, and show the response of the web service.
I googling to expose my app as a web service and send and retrieve XML data.
But I am very confused and I don't know where to start or which django package to use(PyXML,DJANGO REST).
I am not sure if I need to build a web API or I can consume the web service without the web api.
Can someone give some advices to achieve this task.
Answer: You don't need to build a web API if all you want to do is consume someone else's API. Just retrieve the data - with something like requests - and use it in your Django app.
Comment: the question is how to do that. how can I send the request from a form and how to retrieve the data.

Consume web service with DJANGO


Why did my cooktop trip the breaker when I cleaned it with too much water?

Question: I have a problem with my miele built-in gas cooktop. I was cleaning it with baking soda and stabilized hydrogen peroxide, afterwards, I rinsed it with water. I was using too much water and one of the burners made a pop sound and I can feel mild heat around the burner. And following that,  the electricity in my whole unit was tripped.
I called in an electrician and he switched on the electricity inside the switch board outside my apartment unit and attempted to turn on the stove. Again, a pop and electricity tripped back off. He plugged out the power cord and asked me to call an electrical appliance repair tomorrow.
What happened inside the stove? Have the fuse inside the stove blown? How does it cause the electricity switch to tripped?
Comment: It may be as simple as drying everything out (with the power off) or as expensive as a blown control board sometimes as high as 200$. If a fuse blew it should have opened the circuit and not tripped the breaker. If you can examine the wires look for the cause of the  "pop" the water could have started conducting and dammaged the insulation, now with dammaged insulation it keeps shorting and tripping the breaker as 1 possibility.
Comment: Simple answer: we don't know. We'd need to look inside the cooktop. And we can't. I'm going to suggest that you call an electrical appliance repairer.
Answer: The water apparently got inside the stove and caused a short. If there was a fuse inside there that blew, the breaker wouldn't have tripped. The breaker is detecting the excessive current to the appliance and switching off the power before the wiring inside the wall overheats and start a fire.
The best solution, as the electrician indicated, is to have the appliance repaired or replaced. Until then, leave it disconnected since it's a safety hazard. Without opening the appliance and inspecting the connections, it's difficult to say exactly what inside of it has failed.
Answer: I spilled water in my cooktop yesterday and it kept tripping the breaker.  I was going to take the unit out and check the fuse/etc, but I decided first to try using a hairdryer to dry in the knobs and it worked like a champ - no repair call needed this time!
Answer: I'm in Australia so terminology might be different. At the so called fuse box the RDC pretection was tripping. I  turned all the circuits off and one by one turned them on. And the. By turning off appliances worked out which was tripping the safety.
Gas cooktop Electrolux.
Pulled the trivets off. I had been cooking this evening and a wet lid I had stood upright at the back had dropped onto the glass surface. And the rear left hand burner was in a slight pool of moister. So I fired it.  To try to dry it out faster (and not wait U told tomorrow) I have Used the burner (lit manually) and I have a ppo of something on the stove cooking now so hopefully using the burner for half an hour will help to dry it out.
If it doesn't work now I will try again I the morning. And maybe try drying it out with a hair dryer.
Once upon a time about 16 years ago I ckea er the front panel of a microwave with too much "spray and wipe" and it failed. But I let it thoroughly dry out and it came back to life.  A perfect strategy when parts or electrician aren't available..... As long as you're protected by a good safety.
Here there is the RDC. And the fuse.
Comment: This doesn't really add anything to the answers already provided. If you still have a problem, you can post a qustion.
Comment: If you do post this as a question, please be sure to edit it for clarity. It's possible that some of those terms are either common in Aus, or are typos - if they're common Aus terms, you may want to look up synonyms for the rest of the world, or if they're typos, correct them before posting.


What is the difference between url() and tuple for urlpatterns in Django?

Question: So in Django the two lines of url code below work the same:
urlpatterns = patterns('',
    url(r'^login/$', 'django.contrib.auth.views.login'),
    (r'^login/$', 'django.contrib.auth.views.login')
)

AFAIK, the only difference is I can define name='login' so I can use it for reversing url. But besides this, is there any other differences?
Comment: to clarify, it is the `url` prefix that allows you to define the name
Answer: There is no difference whatsoever. Have a look at the patterns function in django.conf.urls.__init__.py, if your url is a list or tuple then it is wrapped up by the url function anyway before being appended to the list of available patterns.


Different ways to get italic formatting?

Question:
Seems familiar, doesn't it?
But when I type something like this
Italic was caused by a pair of single *
since it should be pair of double **
Is this normal?
Answer: The formatting help page gives these examples:
<code>*This is italicized*, and so is _this_.
**This is bold**, and so is __this__.
Use ***italics and bold together*** if you ___have to___.
</code>
which render as
This is italicized, and so is this.
This is bold, and so is this.
Use italics and bold together if you have to.
So: one asterisk or underscore for italic, two for bold.
The small box in the sidebar only gives some examples of markup, it is not a comprehensive reference.
Comment: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet very good an visual reference for markdown


Can be this lisp function be implemented recursively?

Question: The goal of this function is to generate the cartesian product of 2 list.
For example

(combo (list 1 2 3) (list 4 5))  => (1 4) (1 5) (2 4) (2 5) (3 4) (3 5)
(defun combo (l1 l2)
(let    (
            (res (list))
        )
    (dolist (elem1 l1 res)
        (dolist (elem2 l2 res)
            (setq res (append res (list(list elem1 elem2))))
        )
    )
)

)

How could be implemented recursively?
Comment: Yes, it can be implemented recursively.
Comment: Note that your function is very inefficient. Check the use of APPEND.
Answer: A simple recursive implementation would be to use two helper functions; one to traverse through L1 (%COMBO in the code below), calling another function to pair an element with each element from L2 (%PRODUCT):
(defun combo (l1 l2)
  (labels ((%product (el list &optional (acc (list)))
             (if (endp list)
                 acc
                 (%product el (rest list) (cons (list el (first list)) acc))))
           (%combo (l1 l2 &optional (acc (list)))
             (if (endp l1)
                 (nreverse acc)
                 (%combo (rest l1) l2 (nconc (%product (first l1) l2) acc)))))
    (%combo l1 l2)))

The iterative approach is both simpler and more efficient though. Instead of using APPEND in a loop, you should just reverse the list at the end.
(defun combo (l1 l2)
  (let ((res (list)))
    (dolist (e1 l1 (nreverse res))
      (dolist (e2 l2)
        (push (list e1 e2) res)))))

You could also just use the MAP-PRODUCT function from Alexandria:
CL-USER> (ql:quickload :alexandria)
;=> (:ALEXANDRIA)
CL-USER> (use-package :alexandria)
;=> T
CL-USER> (map-product #'list (list 1 2 3) (list 4 5))
;=> ((1 4) (1 5) (2 4) (2 5) (3 4) (3 5))

Comment: It's perfect. Thanks. :)
Comment: What is the role of (nreverse res)  in your second solution?
Comment: @rnso `PUSH` adds an object to the front of the list, so the list has to be reversed at the end to get the results in order. Using `APPEND` in a loop is bad because every time you call it, you're creating a new copy of the list. Lists in lisp are singly linked, so you can make a new list with elements added to the front without having to copy or modify the tail, but adding anything to the back requires either creating a new copy, or destructively modifying the list. Using `PUSH` and `NREVERSE` is the usual Common Lisp idiom for building a list of results.
Answer: It seems ideal for Prolog, though data has to be put differently:
items1(1).
items1(2).
items1(3).

items2(4).
items2(5).

?- findall((A,B), (items1(A), items2(B)), L).
L = [ (1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)].


What is a good example of self-answer to your own question?

Question: There's an option when asking a question were you can answer your own question, as you might know.
Is there a good example in this site of such a question/answer? How exactly should it be used?
Answer: I ran a query on the Stack Exchange API (code). Here are the 10 highest-scoring self-answers on questions from the past 5 years on Linguistics Stack Exchange (overall there were several hundret):

What is the contribution of Tocharian to the reconstruction of Proto-Indogermanic?
When did Nguni languages like Zulu adopt capitalisation of proper noun roots?
The current status of Irish Gaelic in Ireland
How does Kaldi compare with Mozilla DeepSpeech in terms of speech recognition accuracy?
Suppletion of Spanish "matar" (to kill) by "morir" (to die) in the passive
Word length as a function of word frequency?
Apical postalveolar approximant [ɹ̺] and retroflex approximant [ɻ]: What is the difference?
Early Middle English diphthongs
In what language and when did people first start saying "Good morning" / "Good Day" as a greeting?
Did PIE *h3 cause voicing in any other words than the "drink" word?

Obviously, don't abuse this functionality to farm reputation by repeately posting questions you already know the answer to. While self-answers along the lines of user6726's description may be useful for canonical answers to questions that are asked very frequently, Stack Exchange lives on the exchange between askers and answeres, rather than monologues of information written by a single person.
The prototypical use case of answering one's own question would be if over time, possibly by following up on the comments and answers by others, you managed to find an answer to your question on your own and would like to share the information for future readers.
Answer: This question has been discussed on meta: Can I answer my own questions, even if I knew the answer before asking?. The consensus seems to be that it's perfectly fine. In fact, if it is done with just one user account, it is less of a headache for me. As a moderator, some of my biggest headaches here are people who create multiple accounts, ask a trivial question from one account, answer from another account, and farm karma for both accounts. As long as it's all done with one account, I'm confident votes from other users will help promote worthwhile contributions.
If you see a better answer than your own, please accept that instead.
As for good examples here, I don't know. Some of the Q&As linked from the meta site show examples not related to linguistics, but at least demonstrate that good questions and answers accrue a lot of karma.
Answer: I don't know of any examples here, but I do know of them from Law SE. There function is to serve as a basic reference answer on a particular topic, and is usually tied to a tag. An example is "fair use", which people generally don't understand, and it is useful to have a technical summary question and answer. The "fair-use" wiki links to 4 fair-use questions and answers, one of which is a user who q'd and a'd her own question (it's the first q in the wiki), and it is a frequently-cited background source on the topic.
Such self-answers are good in the case of repeat questions, especially if users tend to ask very specific versions of the same basic question – an expert can write a general-enough question with answer so that many questions on that topic are thereby answered. It's not obvious to me that we get many questions that can be dealt with that way. For instance, we get a number of questions about "phonetic reasons for X", but there is no general answer, unless one plans to write a book on the topic (not appropriate for SE).
Comment: These are often called "canonical questions" and can be very useful to have on a SE site.


Unparseable date exception Creating a date from two objects

Question: i'm using JXDatePicker and JSpinner to input date and time from the user.
I then need to format it back to one date in a long millisecond format.
with input of 03/09/2014 in JXDatePicker , the output from the JXDatePicker  is:
Wed Sep 03 00:00:00 IDT 2014

and with input of 11:00 in JSpinner, the output from JSpinner is :
Thu Jan 01 22:33:00 IST 1970

I need to combine both this outputs to get the full date , and then convert it to a long in millis.
I use this code:
SimpleDateFormat f = new SimpleDateFormat("DDD MMM dd yyyy hh:mm:ss");
String string_date =
         datePicker.getDate().toString().substring(0, 11) +
         datePicker.getDate).toString().substring(24, 28) +
         hourPicker.getValue().toString().substring(10,19);
Date d;
    try {
        d = f.parse(string_date);
    } catch (ParseException e3) {
        e3.printStackTrace();
    }

and get this exception:
java.text.ParseException: Unparseable date: "Wed Sep 17 2014 12:44:22"
    at java.text.DateFormat.parse(Unknown Source)
    at MainFrame.sendButtonActionPerformed(MainFrame.java:598)
    at MainFrame.access$3(MainFrame.java:574)
    at MainFrame$3.actionPerformed(MainFrame.java:434)

please help !
thanks,
Dave.
Answer: In order to parse Wed use 'EEE' like show below, list of valid flags are here
class Test
 {

     public static void main(String[] args) throws ParseException {
         SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss");
         String string_date = "Wed Sep 17 2014 12:44:22";
         System.out.println(f.parse(string_date));

     }

  }


Set of points at which sequence of measurable functions converge (another approach)

Question: Question is to prove that :
Set of all points at which a sequence of measurable functions converge is a measurable set..
What i have tried is as follows :
We are looking at the following set :
$$\{x\in X : (f_n(x)) \text{converges}\}$$
Which is same as
$$\{x\in X : (f_n(x)) \text{is cauchy}\}$$
Which is same as
$$\{x\in X : \text{given $\epsilon>0$ there exists $N\in \mathbb{N}$ such that $|f_n(x)-f_m(x)|<\epsilon$ for all $m,n\geq N$}\}$$
I want to write this as unions and intersections of measurable sets and then conlcude this is measurable.. Something like :
$$\bigcap_{p\in \mathbb{R}??}\bigcup_{m,n\in \mathbb{N}??}\{x:|(f_n-f_m)(x)|<p\}$$
Now, As $f_n,f_m$ are measurable so is $f_n-f_m$ and so is $|f_n-f_m|$ and  $\{x:|(f_n-f_m)(x)|<p\}$ being inverse image of open set is measurable..
I am not so sure how to write that as unions and intersections...
Answer: $(f_n(x))_n$ is cauchy means that for any positive integer $p$, there exists one integer $k$, such that for all $m > k$ and $n > k$ we have $|f_n(x) - f_m(x)| < \frac{1}{p}$.
Since it's for any positive integer p, we should have something like $\cap_{p=1}^{\infty}$.
And there exists a $k$ such that blabla means $\cup_{k=1}^{\infty}$ blabla..., i.e. for one k in $\{1,2, \cdots\}$, blabla is ok
For all $m,n$ greater than $k$ is $\cap_{m > k, n>k}$.
So finally $(f_n(x))_n$ is cauchy means that $x$ is in the set
$\cap_{p=1}^{\infty} \cup_{k=1}^{\infty}\cap_{m > k, n>k}\{x: |f_m(x) - f_n(x)| < \frac{1}{p}\}$
To resume, when there is "for any, for all", use intersection; when there is "exists", use union
Comment: i understand this partially... please elaborate it a bit... I usually get stuck at this point only...
Comment: You mean which point that is still not clear?
Comment: now it is fine..
Comment: Thanks a ton times for the last line!
Answer: The idea is good. But use a different definition of continuity which is in fact equivalent: Namely
$$\{x\in X : \text{given $\mathbb N\ni k>0$ there exists $N\in \mathbb{N}$ such that $|f_n(x)-f_m(x)|<\frac{1}{k}$ for all $m,n\geq N$}\}$$
Then write this as
$$\bigcap_{k\in \mathbb{N}}\bigcup_{N\in\mathbb N}\bigcap_{m,n\geqslant N}\left\{ x:\left|(f_n-f_m)(x)\right|<\frac{1}{k}\right\}$$
As you said the set  $\left\{ x:\left|(f_n-f_m)(x)\right|<\frac{1}{k}\right\}$ is measurable and hence you're done, since you have a countable union respectively intersection of measurable sets. This is why you use the different definition of continuity.
Comment: Say an element $x$ is in $A_n$ for all $n$, then $x\in \bigcap _n A_n$ by unwinding the definition. Similarly, if there exists $n$ such that $x\in A_n$ then $x\in \bigcup_n A_n$. It is only another way of writing things down.
Comment: I do not understand how do you write that as unions and intersections... please explain that part...


Hashing vs composite PK to identify if it is an object having attributes which are unique(as a combination)

Question: Requirement : To create a new ID if the combination of attributes does not exist and reusing the ID if the same combination is found.
Two approaches I could think of is using hashing and then bounding an ID to the hashCode.
OR
using composite PK to check if a row is distinct or not.
What could be the best way to handle it keeping in mind performance and memory.
For Ex.
<code>class Lens{
   String axis;
   String cylindrical;
   String spherical;
   float height;
   float width;
   String package;

    ...... and few more
}
</code>
Suggestions for any other approach appreciated.
Comment: How do you plan to generate the hash code? You will have to use composite PK to use it as a key. So why take an extra step. Use composite primary key
Comment: I can generate using SHA256. I can bind a new ID to a new hashcode but not as composite PK. I cannot have composite PK because I will be searching based on hash only.
Answer: I would suggest hashing instead of composite PK mainly because of below reasons:

If in future you want to add any other key in hashing you can do it without changing db table query and changing db table query to add new column in composite PK could end up effecting your previously stored data.
If you use hashing you can validate data before storing by comparing your hash with already present hash in DB but if you are using composite PK you will get the error for duplicate data at the time of storing.

Moreover, I guess performance and memory will not effect that much for both the approaches.


I want to build an interactive machine learning system to classify items using as much readily-available software as possible

Question: I have a complex problem I want to solve with machine learning, but because I'm new to machine learning, I'm going to practice by trying to solve a really simple problem first. The fundamental question is: How much of this can I create by downloading/customising readily available (free or commercial) software?
Imagine a system that understands animals in the zoo. I'll track a few simple characteristics of them: what do they eat? are they nocturnal or diurnal? How do they move (slither, swim, fly, walk, crawl), how many legs do they have? etc. Maybe a dozen attributes per animal and probably all attributes known in the training set.
I want to prime this system with some facts about a bunch of animals and I'll tell it that these are true facts. Then, I basically want to say "OK, I have a new animal" and have it ask me about the animal. Based on what it knows about the data it has, I want it to prioritise questions (i.e., ask the most useful ones first). As it learns about the new animal, I want it to start guessing at the answers. E.g., "Is it nocturnal? I think, with 68% confidence, that it is" and I'll tell it "yes, you're right".
I will want to add new attributes about animals. Maybe whether or not they are predators. Obviously I will have to provide data to start with, but I want the system to be adaptable enough to take on new attributes like this and gradually start to build confidence in its correlations as it acquires data.
An interesting similar system is at 20q.net, which plays "20 questions" and is very good. I'm not trying to play that game, per se, but that's the kind of interactivity I'm looking for. My hard problem will look a bit like the 20 questions problem. There will be hundreds and hundreds of known attributes. Any given 'thing' might have only a few dozen for which the answers are known, and the answers to the other hundreds will simply be unknown. Based on what is known, the system that solves the hard problem will have to select questions to ask get more information.
I've seen Weka and I've even loaded a few data sets into it. Does this seem like the right sort of engine to build my interactive system on? Are there toolkits (Weka or otherwise) that make building such a system straightforward? I.e., how much of this can I just download (or purchase) and customise, and how much am I going to have to build myself?
Comment: I am reasonably knowledgeable about machine learning, and I am skeptical that any off the shelf packages are going to make your problem particularly easy.  The closest well studied problem that I know of is active learning, where the system asks you to label instances rather than features of a particular instance.
Comment: Sounds like you need a decision tree learning. http://en.wikipedia.org/wiki/Decision_tree_learning
Answer: Hello :) Nice to meet you. I'm trying to do something similar.
I have researched the necessary data types required for a program in order to understand, learn and, therefore, ask (to learn some more).
My approach is to teach the program just like a little baby. But a little baby gets bored, distracted and doesn't get it the first time. So a computer program would act the same, until it has the basic tools for understanding, like language.
My current model is this

create a (blank) database; -- this database has some tables like : words, sentences, actions etc.
create a program that can receive input (sentences); parses the sentences, saves words, tries to make sense of the sentence (see python nltk); if the sentence requires output then try to search for output by looking in the database for saved actions. If there are no actions then the program asks the user for the result or hints. If you build a script that searches the answer on the web (good luck) then the program asks the user if it is allowed to look-up the answer online.
If the program cannot resolve the users' request, then it builds up interest on that request.
I'm also trying to create a tree of synapses based on the input-output process. EG: the table of synapses has these fields : 1. id; 2. id_object (more on that later) 3. id_parent (the id of the parent synapse). This would be useful if, say, you would want the program to be multi-tasking and remember the discussions so that you would not have to repeat every command over and over.

For instance: I'll prompt the program to search a "THING" for me and "PERFORM" an "ACTION" with that "THING". This requires RAM-like learning (temporary memory) in order to firstly search for the "THING" and (after and IF it finds it) "PERFORM" the (heh based on the programs' synapses history) best matched "ACTION" that it could find.
This is not an answer to your question because you need to understand databases and nltk before you can continue. This is my concept (or approach) of creating a literally learning machine.
Cheers!


Logic operators behavior in JavaScript

Question: I'm currently learning JavaScript (Python background) and I'm having trouble on the logic behind stacking logic operators.
Let me explain.
In Python (61 <= 81 < 81) would output False. Which makes sense since it's basically (61 <= 81 AND 81 < 81).
But for some reason this same condition (61 <= 81 < 81) outputs True in JavaScript, almost as if it is reading it as (61 <= 81 OR 81 < 81) which is not expected. On the other hand if I explicitly write (61 <= 81 && 81 < 81) the output is False as expected.
Could anyone explain?
Comment: It works like `a < b && b < c`.  You cannot do `a < b < c` in JavaScript.
Comment: Honestly in this case it's Python that's kind-of weird.
Answer: The evaluation of 61 <= 81 < 81  is not comparing 81.
You have (61 <= 81) which is true and then it is true < 81
The only way to do what you want is to break it up into two checks with an AND
61 <= 81 && 81 < 81

Logic operators behavior in JavaScript


How to implement back arrow(Up Button) in the vertical center of the Toolbar

Question: When adding back button in Toolbar in landscape mode, back button is not in the vertical center of an toolbar.
Only thing I do is in android manifest:
<activity
        android:name="tack.hardcode.com.tack.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape">

Here is my toolbar code
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3F51B5">

   </android.support.v7.widget.Toolbar>

I also have v21.styles implemented and i have include toolbar in my mainActivity.
What else should I do?!
Comment: Do you want to implement back arrow (I.e. Up navigation) or do you want it to be in vertical center? @KiZo
Comment: It is already implemented, i only want to be in vertical center of an app_bar
Comment: Can you post your toolbar code? I mean xml.
Comment: I have posted answer.
Answer: You have set toolbar height to wrap_content causes this problem. Actionbar height is always 56dp in portrait and 48dp in landscape. (For mobile devices)
If you are setting wrap_content as height for toolbar then it took same height for both portrait and landscape. So in portrait mode, up button remains in center while in landscape it goes up and empty space added to the bottom.
So, change toolbar height to android:layout_height="?actionBarSize".
By changing this, toolbar takes the same height as actionbar for both in portrait and landscape mode.
Hope it helps to you. And I hope now you are clear.
Comment: it works, thank u very much, although i do not understand why am I using actionBarSize for Toolbar?
Answer:     Toolbar mToolBar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar (mToolBar);
    ActionBar actionBar = getSupportActionBar ();
    actionBar.setElevation (5);
    actionBar.setDisplayHomeAsUpEnabled(true);

Comment: I already did this and back button is still not in vertical center of an app_bar. Back button is working nice but i want it to be in vertical center on the left side. Now it is on the left side but not in vertical center.